Django url反向解析与路由分发名称空间

时间:2022-10-13 04:37:22

url反向解析

url.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^loginxxx/', views.login,name='login'),
    url(r'^indexxx/', views.index, name='index'),
]

视图views.py

from django.shortcuts import render, HttpResponse, redirect
from django.core.urlresolvers import reverse


def login(request):
    if request.method == 'POST':
        if 1:
            return redirect(reverse('index'))
    return render(request, 'login.html')


def index(request):
    return HttpResponse('this is index')

模板 templates/login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form action="{% url 'login' %}" method="post">
    {% csrf_token %}
    <input type="text" name="user" placeholder="username">
    <input type="password" name="pwd" placeholder="password">
    <input type="submit" value="提交">
</form>
</body>
</html>

总结:

模板中:

url(r'^loginxxx/', views.login,name='login'),
...
<form action="{% url 'login' %}" method="post">  <!-- 解析成loginxxx/ -->
...
</form>

  

视图中:

url(r'^indexxx/', views.index, name='index'),
...
from django.shortcuts import render, HttpResponse, redirect
from django.core.urlresolvers import reverse


def login(request):
    if request.method == 'POST':
        if 1:
            return redirect(reverse('index'))  # 解析成 indexxx/
    return render(request, 'login.html')

  

路由分发

项目下的urls.py

from django.conf.urls import url,include
urlpatterns = [
    url(r'^app01/', include('app01.urls')),
    url(r'^app02/', include('app02.urls')),

]

app01.urls.py

from django.conf.urls import url
from app01 import views

urlpatterns = [
    url(r'^index/', views.index, name='index'),

]

app02.urls.py 

from django.conf.urls import url
from app02 import views

urlpatterns = [
    url(r'^index/', views.index, name='index'),

]

views:(app01跟app02的一样)

from django.shortcuts import HttpResponse
from django.core.urlresolvers import reverse


def index(request):
    return HttpResponse(reverse('index'))

这样,问题来了当我们访问http://127.0.0.1:8000/app01/index/的时候,期待的结果是/app01/index/ 但是url反向解析的结果却是/app02/index/

解决办法:名称空间

名称空间

项目下urls.py
from django.conf.urls import url,include
urlpatterns = [
    url(r'^app01/', include('app01.urls',namespace='app01')),
    url(r'^app02/', include('app02.urls',namespace='app02')),

]

app01.urls.py
from django.conf.urls import url
from app01 import views

urlpatterns = [
    url(r'^index/', views.index, name='index'),

]

app02.urls.py
from django.conf.urls import url
from app02 import views

urlpatterns = [
    url(r'^index/', views.index, name='index'),

]

app01.views.py
from django.shortcuts import HttpResponse
from django.core.urlresolvers import reverse


def index(request):
    return HttpResponse(reverse('app01:index'))

app02.views.py
from django.shortcuts import HttpResponse
from django.core.urlresolvers import reverse


def index(request):
    return HttpResponse(reverse('app02:index'))