Olivia Oddo
Olivia Oddo

Reputation: 35

Trouble with namespace. Two different Urls are rendering the same view

I am having trouble with my urls. I have one app called users that has two models, Salon and Stylist. The url /stylists and /salons is rendering the same view (stylist_results) however /salons should render salon_results and I cannot figure out why. I think there may be something wrong with the way I am using namespaces.

users/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')

Upvotes: 0

Views: 47

Answers (3)

Alasdair
Alasdair

Reputation: 308789

Split your stylist and salon views into two separate modules. You might consider creating two different apps, but you don't have to.

users/stylist_urls.py

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

users/salon_urls.py

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

Then update your project's urls.py with the new modules:

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

At the moment, the regexes for your salon URLs are exactly the same as the stylist URLs, so the salon URLs will always match first.

Upvotes: 1

Mithilesh Gupta
Mithilesh Gupta

Reputation: 2930

You are specifying same set of url's(users.urls) for salons and stylists here:

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

Upvotes: 1

iklinac
iklinac

Reputation: 15728

What did you expect to happen

You are including same users/urls.py in urls.py

It does following:

find me /stylist/ => go into included urls find first occurance of url(r'^$', views.stylist_results, name='stylist_results'),

renders that view

same thing happens with /salons/

URL Dispatcher documentation

Upvotes: 0

Related Questions