实例分析,过程使用URL反向解析知识:
Django开发_12_URL反向解析、重定向-CSDN博客y
一、实例代码
(一)主路由urls.py:
path("work4/", include("work4_app.urls",namespace="work4")),
(二)app内urls.py:
from django.urls import pathfrom work4_app.views import fill_hobby, show_hobbyapp_name = "work4"
urlpatterns = [path("fill/", fill_hobby, name="fill"),path("show/", show_hobby, name="show"),]
(三)views.py:
from django.shortcuts import render, redirectdef fill_hobby(request):return render(request, "hobby.html", locals())def show_hobby(request):hobby = request.POST.get("hobby")return render(request, "post.html", locals())
(四)html文件:
hobby.html:
<form action="{% url 'work4:show' %}" method="post">{% csrf_token %}<br>输入你的爱好:<input type="text" name="hobby"><input type="submit" value="提交">
</form>
post.html:
"我的兴趣爱好是:{{ hobby }}
二、过程分析
hobby.html:
urls.py:
views.py:
效果展示: