Python3.6.7
pip install django==2.2.6
django-admin startproject yourproject
python manage.py startapp app1 python manage.py startapp app2
需要将app注册到项目的settings.py中
重点关注name声明
from django.urls import path from . import views urlpatterns=[ path('search/',views.search,name='diary_search'), path('home/',views.home), ]
重点关注include写法、namespace声明
from django.contrib import admin from django.urls import path,include urlpatterns = [ path('admin/', admin.site.urls), path('skynet/', include(('skynet.urls','skynet'),namespace='skyent')), path('diary/', include(('diary.urls','diary'),namespace='diary')), ]
重点关注form action
<form action="{% url 'skynet:skynet_search'%}" method="post"> {% csrf_token %} <input type="text" name="keywords"> <button type="submit">提交</button> </form>
from django.shortcuts import render #Create your views here. def home(request): return render(request,'index.html') def search(request): keywords=request.POST.get('keywords') print(keywords) return render(request,'index.html')
总结:经过上述操作可实现django项目多app路由分发,这样做的好处是只需要修改后端路由,前端的路由会随之变化
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理