【问题标题】:Trouble with namespace. Two different Urls are rendering the same view命名空间有问题。两个不同的 Url 呈现相同的视图
【发布时间】:2017-02-05 07:16:45
【问题描述】:

我的网址有问题。我有一个名为 users 的应用程序,它有两个模型,沙龙和造型师。 url /stylists 和 /salons 呈现相同的视图(stylist_results)但是 /salons 应该呈现 salon_results 我不知道为什么。我认为我使用命名空间的方式可能有问题。

用户/urls.py

from django.conf.urls import url

#import views from the current directory
from . import views

urlpatterns=[
    url(r'^$', views.stylist_results, name='stylist_results'),
    url(r'^(?P<pk>\d+)$', views.stylist_detail, name='stylist_detail'),
    url(r'^$', views.salon_results, name='salon_results'),
    url(r'^(?P<pk>\d+)$', views.salon_detail, name='salon_detail'),
]

users/views.py

from django.shortcuts import get_object_or_404, render

from .models import Salon, Stylist
# Create your views here.

def salon_results(request):
    salons = Salon.objects.all()
    return render(request, 'salons/salon_results.html', {'salons': salons})


def salon_detail(request, pk):
    salon = get_object_or_404(Salon, pk=pk)
    return render(request, 'salons/salon_detail.html', {'salon': salon})


def stylist_results(request):
    stylists = Stylist.objects.all()
    return render(request, 'stylists/stylist_results.html', {'stylists': stylists})

def stylist_detail(request, pk):
    stylist = get_object_or_404(Stylist, pk=pk)
    return render(request, 'stylists/stylist_detail.html', {'stylist': stylist})

urls.py

from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from . import views

urlpatterns = [
    url(r'^salons/', include('users.urls', namespace='salons')),
    url(r'^stylists/', include('users.urls', namespace='stylists')),
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home, name='home'),
]


urlpatterns += staticfiles_urlpatterns()

views.py

from django.shortcuts import render


def home(request):
    return render(request, 'home.html')

【问题讨论】:

  • 它总是执行第一个匹配的 url - r'^$', views.stylist_results,。创建两个单独的文件。

标签: python django django-templates django-views


【解决方案1】:

将您的造型师和沙龙视图拆分为两个独立的模块。您可能会考虑创建两个不同的应用程序,但您不必这样做。

用户/stylist_urls.py

urlpatterns = [
    url(r'^$', views.stylist_results, name='stylist_results'),
    url(r'^(?P<pk>\d+)$', views.stylist_detail, name='stylist_detail'),
]

用户/salon_urls.py

urlpatterns = [
    url(r'^$', views.salon_results, name='salon_results'),
    url(r'^(?P<pk>\d+)$', views.salon_detail, name='salon_detail'),
]

然后使用新模块更新您项目的urls.py

urlpatterns = [
    url(r'^salons/', include('users.salon_urls', namespace='salons')),
    url(r'^stylists/', include('users.stylist_urls', namespace='stylists')),
    ...
]

目前,沙龙 URL 的正则表达式与造型师 URL 完全相同,因此沙龙 URL 将始终首先匹配。

【讨论】:

    【解决方案2】:

    您在这里为沙龙和造型师指定相同的一组 url(users.urls):

    url(r'^salons/', include('users.urls', namespace='salons')),
        url(r'^stylists/', include('users.urls', namespace='stylists')),
    

    【讨论】:

      【解决方案3】:

      你预计会发生什么

      您在 urls.py 中包含相同的 users/urls.py

      它执行以下操作:

      找到我/stylist/ => 进入包含的网址 找到第一次出现的 url(r'^$', views.stylist_results, name='stylist_results'),

      呈现该视图

      同样的事情发生在 /salons/

      URL Dispatcher documentation

      【讨论】:

        猜你喜欢
        • 2020-07-13
        • 2011-04-10
        • 2018-07-27
        • 2014-01-12
        • 1970-01-01
        • 1970-01-01
        • 2011-05-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多