Django3 编写views/使用模版/设置404/URL命名空间=3
一、完善项目
1.1 编写更多的Views
编写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)
将上述views加入到路由:
from django.urls import path
from . import views
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
再改变一下views让主页返回最后5个poll:
from django.http import HttpResponse
from .models import Question
def 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)
# Leave the rest of the views (detail, results, vote) unchanged
由于这里只添加了3个问题,所以只显示了3个。
1.2 编写/使用模版
1、首先在polls目录添加templates文件夹。templates描述了Django如何加载和渲染模版。
默认设置APP_DIRS
为True
。django会扫描INSTALLED_APPS
下所有templates目录。
2、在templates中创建一个polls,在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 %}
3、然后更新polls/views.py
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))
访问polls如下图:
4、再次更新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)
1.3 返回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})
更新polls/templates/polls/detail.html
{{ question }}
访问具体的问题即可看到内容:
如果访问不存在的id将返回404:
我们可以看到,如果需要从views往templates传值,需要指定字典形式的值传送过去。
当我们需要传送的值非常多的时候,会非常麻烦。
我们可以使用`locals()函数来简化我们的传值工作。
# 使用locals()函数可以将我们本地所有的变量,以字典形式传送过去
return render(request, 'index.html', locals())
更新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})
1.4 继续使用模版系统
更新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>
模版系统使用点查询方式来获取元素的属性。
接下来,我们改变polls/index.html模版中的地址引用:
<!-- 上面一行是之前的,下面一行是相对地址引用 -->
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
这里我们将绝对地址引用改为相对地址引用。
这样的话,当我们在路由配置中改地址的时候,可以不用改模版。
比如我们将polls/urls.py其中一条地址路由匹配改为:
path('okokk/<int:question_id>/', views.detail, name='detail'),
这样的话,我们访问问题具体页面的时候,地址就会变成:
polls/okokk/.....
1.5 URL命名空间
当有多个项目时,views可能相同,URL可能会冲突,这个时候就需要命名空间来避免了。
我们在相应应用的urls.py中添加相应应用名即可:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
然后在模版中引用的时候加上应用名即可:
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)