#coding=utf-8
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
#slug(URL)是模型和视图用来连接的桥梁
# Create your views here.
def index(request):
#return HttpResponse("<h1>欢迎访问新闻网站</h1>")
#return render(request, "index.html", {"columns":Column.objects.all})
return render(request, "index.html", {"is_display_in_list":Column.objects.filter(is_display_in_list=True),
"is_display_in_nav":Column.objects.filter(is_display_in_nav=True)})
"""
根据唯一的slug来访问对应的数据库表的某一项
"""
def column(request, column_slug_tag):
#return HttpResponse("column_slug:{}".format(column_slug_tag))
col = Column.objects.get(slug = column_slug_tag)
return render(request, "news/column.html", {"column": col})
"""
根据唯一的slug来访问对应的数据库表的某一项
"""
def article(request, pkey, article_slug_tag):
#return HttpResponse("article_slug:{}".format(article_slug_tag))
#使用get仅返回一个对象,如果返回多个对象会报
#Multipleobjectsreturned
#要返回多个对象使用filter
#a = Article.objects.filter(slug = article_slug_tag)
#return render(request, "news/article.html", {"article": a[0]})
#要返回一个对象请使用get
a = Article.objects.get(id = pkey)
return render(request, "news/article.html", {"article":a})
"""
可以通过这种方式来对一个/xxx/片段的URL进行分离成不同的参数
来进行解析
"""
def re(request, a_tag, b_tag):
return HttpResponse("a_tag={} b_tag={}".format(a_tag, b_tag))
django开发要求-视图
最新推荐文章于 2023-03-20 00:00:00 发布