python_Django_3:Views and templates
2017-04-23 16:45
351 查看
一、视图
添加更多的视图,在app的
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)
添加了视图之后,我们需要把相应的
url添加到
app下的
urls.py文件:
from django.conf.urls import url from . import views urlpatterns = [ # 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'), ]
二、模板
创建templates文件夹,创建模板文件在
polls/templates/polls,创建
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 %}
修改视图
from django.http import HttpResponse from django.template import loader from .models import Question def 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))
上面使用的
HttpResponse的方法较为麻烦,可以使用快捷方式。
from django.shortcuts import render from .models import Question def index(request): latest_question_list = 4000 Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context)
三、404
在app下templates/polls/建立文件
detail.html。
修改视图
from django.http import Http404 from django.shortcuts import render from .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})
快捷方式
from django.shortcuts import get_object_or_404, render from .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})
修改
detail.html文件
<h1>{{ question.question_text }}</h1> <ul> {% for choice in question.choice_set.all %} <li>{{ choice.choice_text }}</li> {% endfor %} </ul>
四、移除硬url
在html中写url的时候,不要使用硬url,如:<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
要如下:
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
在添加url
的命名空间
在app的
urls.py中添加
app_name
from django.conf.urls import url from . import views app_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'), ]
添加
url的命名空间
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
相关文章推荐
- 二 Django 1.5.4 Views and Templates
- python_Django_4:Forms and generic views
- django(views、urls、templates)
- Python and Django 001 MySQL
- python后台架构Django教程——templates模板
- Python的Django框架完成一个完整的论坛(3.创建app的models.py、views.py源码)
- Python and django(四)--详解python中的数字和序列
- lynda.com教程之Learing Python and Django零基础搭建Django项目
- 10.28,python,django中templates的奇偶性,python,swift多语言文件转android多语言文件
- Professional Python Frameworks: Web 2.0 Programming with Django and Turbogears
- Python_django_forget_account_and_password
- [Django] Get started with Django -- Install python and virtualenv
- The Definitive Guide To Django 2 学习笔记(一) Views and UrL confsRL
- python and Django(一)
- Python and django(四)--详解python中的数字和序列
- Python Django(自写Views)
- 读书笔记:Django 模板开发(二)Views, URLs, and Generic Views
- install python 3.2, django and eclipse pydev plugin in mac for python devolopment
- python and Django(一)
- Django, lxml, WSGI, and Python sub-interpreter magic