首页 > 代码库 > django入门-part3
django入门-part3
完全翻译自官方文档 https://docs.djangoproject.com/en/1.10/intro/tutorial03/
本节讲视图
我们设计一个简单投票页面,它有如下3个功能
1. 显示要投票的问题
2. 点击"投票"按钮提交你的选项
3. 展示这个投票的结果
polls/views.py 代码如下:
def detail(request, question_id): return HttpResponse("You‘re looking at question %s." % question_id) #显示要投票的题目def results(request, question_id): response = "You‘re looking at the results of question %s." # 显示投票结果 return HttpResponse(response % question_id)def vote(request, question_id): return HttpResponse("You‘re voting on question %s." % question_id) # 提交投票选项内容
polls/urls.py 代码如下
from django.conf.urls import urlfrom . import viewsurlpatterns = [ # ex: /polls/ url(r‘^$‘, views.index, name=‘index‘), # ex: /polls/5/ url(r‘^(?P<question_id>[0-9]+)/$‘, views.detail, name=‘detail‘), # ex: /polls/5/results/ url(r‘^(?P<question_id>[0-9]+)/results/$‘, views.results, name=‘results‘), # ex: /polls/5/vote/ url(r‘^(?P<question_id>[0-9]+)/vote/$‘, views.vote, name=‘vote‘),]
一个视图只会返回 HttpResponse 或 Http404
下面写一个从数据库获取内容的view
from django.http import HttpResponsefrom .models import Questiondef index(request): latest_question_list = Question.objects.order_by(‘-pub_date‘)[:5] output = ‘, ‘.join([q.question_text for q in latest_question_list]) return HttpResponse(output)
我们在代码里硬编码了页面只返回5条内容,这很外行,所以我们用django的模板系统
django默认会在这个目录结构下读取我们的模板文件
polls/templates/polls/index.html
就是在你的应用目录下创建文件夹 templates ,再在templates里创建一个跟应用名字一样的目录poolls
这个index.html的访问路径就是 polls/index.html
你的index.html内容如下:
{% if latest_question_list %} <ul> {% for question in latest_question_list %} <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li> {% endfor %} </ul>{% else %} <p>No polls are available.</p>{% endif %}
相应的修改你的views.py
from django.http import HttpResponsefrom django.template import loaderfrom .models import Questiondef index(request): latest_question_list = Question.objects.order_by(‘-pub_date‘)[:5] template = loader.get_template(‘polls/index.html‘) context = { ‘latest_question_list‘: latest_question_list, } return HttpResponse(template.render(context, request))
context 就是一个字典, 模板里会使用这个变量
上面的方法稍显麻烦,先加载loader模板文件,再渲染render文件,django为我们提供了一个更方面的命令 render()
继续修改我们的使用,我们使用render重写上面的index()
from django.shortcuts import renderfrom .models import Questiondef index(request): latest_question_list = Question.objects.order_by(‘-pub_date‘)[:5] context = {‘latest_question_list‘: latest_question_list} return render(request, ‘polls/index.html‘, context)
当访问的页面不存在时,我们抛一个404出去
from django.http import Http404from django.shortcuts import renderfrom .models import Question# ...def detail(request, question_id): try: question = Question.objects.get(pk=question_id) except Question.DoesNotExist: raise Http404("Question does not exist") return render(request, ‘polls/detail.html‘, {‘question‘: question})
再精简代码,正常获取和抛404可以用一个方法 get_object_or_404()
from django.shortcuts import get_object_or_404, renderfrom .models import Question
def detail(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, ‘polls/detail.html‘, {‘question‘: question})
类似的还有get_list_or_404(),还有最好用filter()函数代替get(),因为get()会抛一个Http404异常
模板系统开讲
polls/templates/polls/detail.html
<h1>{{ question.question_text }}</h1><ul>{% for choice in question.choice_set.all %} <li>{{ choice.choice_text }}</li>{% endfor %}</ul>
用点来访问变量的属性
来看这段代码,我们在href中硬编码了访问路径
<li><a href=http://www.mamicode.com/"/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
怎么解决呢,在url()方法里加name参数
...# the ‘name‘ value as called by the {% url %} template tagurl(r‘^(?P<question_id>[0-9]+)/$‘, views.detail, name=‘detail‘),...
修改以后,上面的硬编码变成这样
<li><a href=http://www.mamicode.com/"{% url ‘detail‘ question.id %}">{{ question.question_text }}</a></li>
这样即使你修改了你的url路径,那么你的html也不需要修改.
URL命名空间
在一个django项目里可能包含很多应用, 那么就会出现多个url(name="这里的名字有重复"),那么怎么解决呢
from django.conf.urls import urlfrom . import viewsapp_name = ‘polls‘ # <==就是它urlpatterns = [ url(r‘^$‘, views.index, name=‘index‘), url(r‘^(?P<question_id>[0-9]+)/$‘, views.detail, name=‘detail‘), url(r‘^(?P<question_id>[0-9]+)/results/$‘, views.results, name=‘results‘), url(r‘^(?P<question_id>[0-9]+)/vote/$‘, views.vote, name=‘vote‘),]
同时修改你的html
<li><a href=http://www.mamicode.com/"{% url ‘polls:detail‘ question.id %}">{{ question.question_text }}</a></li>
看到区别了吗
好了 本节完
django入门-part3