用Get方法实现文章分类功能

来源:互联网 发布:node.js spring 编辑:程序博客网 时间:2024/06/08 01:20

开发顺序(TMVUT)

Model层需要多少数据字段?
View层根据什么请求,返回什么结果?
Template层如何与用户交互?

M

from django.db import models# Create your models here.class People(models.Model):    name = models.CharField(null=True, blank=True,max_length=200)    job = models.CharField(null=True, blank=True, max_length=200)    def __str__(self):        return self.nameclass Aritcle(models.Model):#新增    headline = models.CharField(null=True, blank=True,max_length=500)#新增    content = models.TextField(null=True, blank=True)#新增    TAG_CHOICES = (        ('tech', 'Tech'),        ('life','Life'),    )    tag = models.CharField(null=True, blank=True, max_length=5, choices=TAG_CHOICES)    def __str__(self):        return self.headline
C:\Users\Administrator\Desktop\standardweb\firstsite>python manage.py makemigrationsMigrations for 'firstapp':  firstapp\migrations\0003_aritcle_tag.py    - Add field tag to aritcleC:\Users\Administrator\Desktop\standardweb\firstsite>python manage.py migrateOperations to perform:  Apply all migrations: admin, auth, contenttypes, firstapp, sessionsRunning migrations:  Applying firstapp.0003_aritcle_tag... OKC:\Users\Administrator\Desktop\standardweb\firstsite>python manage.py runserverPerforming system checks...

V

from django.shortcuts import render, HttpResponsefrom firstapp.models import People, Aritclefrom django.template import Context, Template# Create your views here.def index(request):    print(request)    print('==='*30)    print(dir(request))    print('==='*30)    print(type(request))    queryset = request.GET.get('tag')    if queryset:        article_list = Aritcle.objects.filter(tag=queryset)    else:        article_list = Aritcle.objects.all()#变量article_list储存Article所有的文章    context = {}#新建字典    context['article_list'] = article_list #字典context中的article_list键(html中通过检索键提取文章内容)对应article_list变量的值    index_page = render(request, 'first_web_2.html', context)    return index_page

T

{% load staticfiles %}<!DOCTYPE html><html>    <head>        <meta charset="utf-8">        <title>first web</title>        <link rel="stylesheet" href="{% static 'css/semantic.css' %}"  media="screen" title="no title" charset="utf-8">        <link href="https://fonts.googleapis.com/css?family=Oswald|Raleway" rel="stylesheet">        <style type="text/css">            h1 {                font-family:'Oswald', sans-serif!important;                font-size:40px;            }            body {                font-family: 'Raleway', sans-serif;            }            p {                font-family: 'Raleway', sans-serif;                font-size:18px;            }            .ui.vertical.segment.masthead {                height: 300px;                background-image: url({% static 'images/star_banner.jpg' %});                background-size: cover;                background-position: 100% 80%;            }            .ui.container.segment {                width: 800px;            }            .ui.center.aligned.header.blogslogon {                margin-top: 40px;            }            .ui.center.aligned.header.blogslogon p {                margin-top: 10px;                color: white;                font-size: 10px;            }            .ui.container.nav {                width: 500px;            }        </style>    </head>    <body>        <div class="ui inverted vertical  segment masthead">            <h1 class="ui center aligned header blogslogon" style="font-size:50px;font-family: 'Raleway', sans-serif!important;">                Bloger                <p class="ui sub header">                    everyone has a story to tell                </p>            </h1>        </div>        <div class="ui container nav">            <div class="ui borderless text three item menu ">                <div class="ui simple dropdown  item">                    Categories                    <i class="dropdown icon"></i>                    <div class="menu">                        <a class="item" href="?tag=life">life</a>                        <a class="item" href="?tag=tech">tech</a>                    </div>                </div>                <a class="item">                    Popular                </a>                <a class="item">                    About                </a>            </div>        </div>        <div class="ui divider"></div>        <div class="ui  vertical segment">            {% for article in article_list %}                <div class="ui container vertical segment">                    <a href="#">                        <h1 class="ui header">                            {{ article.headline }}                        </h1>                    </a>                    <i class="icon grey small unhide">10,000</i>                    <p>                        {{ article.content|truncatewords:100 }}                        <a href="#">                            <i class="angle tiny double grey right icon">READMORE</i>                        </a>                    </p>                    <div class="ui mini  tag label">                        {{ article.tag }}                    </div>                </div>            {% endfor %}        </div>        <div class="ui inverted  vertical very padded  segment">            Mugglecoding®        </div>    </body></html>
原创粉丝点击