django的视图有两种,一种function
式,一种式class
式
setting文件里面的ROOT_URLCONF
设置了路由匹配的主url文件
编写第一个视图
polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
然后在应用下的url文件注册url匹配
polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
在项目主url文件里面分配url到应用
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
利用正则匹配url来获取参数
--视图编写定义形参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 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'),
]
注意:
在分组内也可以不用定义变量的名字,形参按顺序接收,如果定义了名字,必须名字对应接收
渲染django模板的低版本模式
from django.http import HttpResponse
from django.template import RequestContext, 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 = RequestContext(request, {
'latest_question_list': latest_question_list,
})
return HttpResponse(template.render(context))
使用render方式渲染模板
polls/views.py
from django.shortcuts import render
from .models import Question
def 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)
--模板语言使用{{}}包括
polls/templates/polls/detail.html
{{ question }}
引发404错误的低级方式
polls/views.py
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})
引发404错误的高级方式get_object_or_404
polls/views.py
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})
处理模板语言中的url连接指向
原始方法
可维护性太差
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
使用反向解析
需要使用name
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
--需要在当前app下的url文件里面的路由规则定义name属性 name=detail
--url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail')
--这样模板语言就会自动填充url,并且将参数question.id 传送到url里面
渲染到其他app下的url
需要使用命名空间namespace
在主路由url文件下添加namespace属性
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
在app下的url文件里面添加name属性
mysite/urls.py
url(r'^specifics/(?P<question_id>[0-9]+)/$', views.detail, name='detail')
修改模板语言为
polls/templates/polls/index.html
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
网友评论