简体   繁体   中英

Django “homepage” app urls.py issue

I am pretty new to Django and when I laid my site out I did it in the following way:

The project is a "portal" of sorts,

  • App 1 "home" is the app that houses the homepage, login, registration and other "site-wide" functionality
  • App 2 "inventory" is a basic asset inventory
  • App 3 "dashboard" is a set of status dashboards based on assets in the inventory

Right now I am trying to add the login functionality and I have a main project urls.py that looks like this:

File: /portal/urls.py

from django.conf import settings
from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', include('home.urls'), name='home'),
    url(r'^admin/', include(admin.site.urls), name='admin'),
    url(r'^inventory/', include('inventory.urls'), name='inventory'),
    url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
    url(r'^capacity/', include('capacity.urls'), name='capacity'),
)

My plan is to use the inclusion of .../home/urls.py to manage any site-wide functionality such as logging in, registering, etc.

Right now the home index view shows just fine, but anything else in .../home/urls.py gives me a 404

File: /home/urls.py

from django.conf.urls import patterns, url
from home import views

urlpatterns = patterns('',
    url(r'^test/$', views.index, name='home_test'),
    url(r'^ajax_login/$', views.ajax_login, name='ajax_login'),
    url(r'^$', views.index, name='home_index'),
)

At this point I suppose my question is two-fold: Am I approaching this correctly? If so, how can I get the desired functionality? If not, how should I change the way things are set up/laid out to do it the correct "best practices" way?

Thanks in advance!

EDIT

Got it working thanks to dt0xff and holdenweb, accepted holdenweb's answer since it was more accurate including the need to put the home url inclusion below the rest.

Here is my new portal/urls.py file for reference

File: /portal/urls.py

from django.conf import settings
from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls), name='admin'),
    url(r'^inventory/', include('inventory.urls'), name='inventory'),
    url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
    url(r'^capacity/', include('capacity.urls'), name='capacity'),
    url(r'^', include('home.urls'), name='home'),
)

The issue is with your first pattern, which will only match the empty URL. So any empty URL will cause the home/urls.py URLs to be analyzed, but only the empty one will match even in that.

Unfortunately if there is no common prefix then that pattern "^" will match every URL (since they all start at the beginning ...).

So you should either use a common prefix for all the pages, or put the home URL specification at the end to give the other URLs a chance to match before it is tested.

Django looks in urls like this:

  1. Get list of root urls - portal/urls
  2. Find first, which matches to current url
  3. If it is include, go to included urls, cutting off matched part

Your problem is here

url(r'^$', include('home.urls'), name='home'),

I mean, here

'^$'

You want url to match "start and then end of url". It works good with root( dunno.com/ ), but not with others, because url will contain something more...

So, just remove $ and you are fine

url(r'^', include('home.urls'), name='home'),

你永远不需要在project / urls.py上添加正则表达式($)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM