Django, urls.py, include doesn't seem to be working

23,769

Solution 1

The problem seemed to be in the django.wsgi file - and the differences in how the standard django.wsgi file loads a python site vs how the development server loads the site. I guess it's a well known issue, that I was unaware of. Thanks everyone for the suggestions.

Alternative django.wsgi file found here: http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html

Solution 2

The problem is that you are using an empty regex ("^" will match anything, including an empty url) to handle an include directive. If you do that, it will always append a first slash at your request path. Considering that on your pnasser.urls does not contain a regex for "/", there is no match for a request on mysite.com.

If you want mysite.com or mysite.com/ to take you to pnasser "index" view, you need to have something like:

from django.contrib import admin

from pnasser.views import index

admin.autodiscover()


urlpatterns = patterns('',
    (r'^/?$', index),
    (r'^pnasser/',include('pnasser.urls')),
    (r'^admin/',include(admin.site.urls)),
)

So, you have:

  • mysite.com => pnasser.views.index
  • mysite.com/ => pnasser.views.index
  • mysite.com/admin => admin page
  • mysite.com/pnasser/ => pnasser.views.index
  • mysite.com/pnasser/home => pnasser.views.home

If this doesn't work, make sure that you have Django's CommonMiddleware installed, and make you have APPEND_SLASH = True (it is the default, so you shouldn't need to mess with this if you don't find it in your settings.py file).

Solution 3

from django.contrib import admin,include
admin.autodiscover()

urlpatterns = patterns('',
        (r'^pnasser/',include('pnasser.urls')),
        (r'^admin/',include(admin.site.urls)),
        (r'^',include('pnasser.urls')),
)

maybe you missed "include" in the first line

Share:
23,769
Prescott
Author by

Prescott

Software engineer in the financial services space

Updated on May 15, 2020

Comments

  • Prescott
    Prescott almost 4 years

    I'm trying to include an additional urls.py inside my main urls - however it doesn't seem to be working. I've done a bunch of searching and I can't seem to figure it out

    main urls.py file - the admin works fine

    from django.conf.urls.defaults import patterns, include, url
    
    from django.contrib import admin
    admin.autodiscover()
    
    urlpatterns = patterns('',
            (r'^pnasser/',include('pnasser.urls')),
            (r'^admin/',include(admin.site.urls)),
            (r'^',include('pnasser.urls')),
    )
    

    I then have a folder pnasser, with the file urls.py with the following:

    from django.conf.urls.defaults import patterns, include, url
    
    urlpatterns = patterns('pnasser.views',
            (r'^$','index'),
            (r'^login/$','login'),
            (r'^signup/$','signup'),
            (r'^insertaccount/$','insertaccount'),
            (r'^home/$','home'),
            (r'^update/(?P<accid>\d+)','update'),
            (r'^history/(?P<accid>\d+)','account_history'),
            (r'^logout/(?P<accid>\d+)','logout'),
    
    )
    

    I'm not sure if I'm maybe missing something else in the configuration. if I visit mysite.com/admin it loads the admin correctly, if I goto mysite or any other url in the views I get 404 page not found:

    Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: 1. ^pnasser/ 2. ^admin/

    The current URL, , didn't match any of these.

    edit settings.py installed apps:

    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        #'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        # Uncomment the next line to enable the admin:
         'django.contrib.admin',
        # Uncomment the next line to enable admin documentation:
        # 'django.contrib.admindocs',
            'pnasser',
    
    )
    

    Update 2

    So, I also tried running my site via the dev server: python manage.py runserver 0.0.0.0:8000 this works. I'm assuming somewhere in my integration with apache using mod_wsgi is the problem. However, I'm not sure where the problem would be

  • Prescott
    Prescott over 12 years
    It appears to be there - unless I've done something wrong here. I've edited my question to include the Installed_apps variable
  • j_syk
    j_syk over 12 years
    So does it work now? If not, try stopping then starting your apache server. I've had problems with urls and settings not updating right away when using a restart command. But stop start seems to help.
  • Prescott
    Prescott over 12 years
    Yes sorry, had to wait a day to mark as answer. I did have to start and restart
  • beerbajay
    beerbajay over 12 years
    How about a link for future users with a similar problem?
  • Prescott
    Prescott over 12 years
    @beerbajay - aboslutely... blog.dscpl.com.au/2010/03/…