2

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

sra
  • 23,820
  • 7
  • 55
  • 89
Prescott
  • 7,312
  • 5
  • 49
  • 70
  • Do all of the views referenced in pnasser's urls.py exist? I seem to recall someone in #django had a problem with that... – eternicode Oct 11 '11 at 23:08
  • 1
    I just did the same exact url scheme in an app. The only difference that I have is that I do not have an `r`, just `("^", include("app.urls")),` Try that? Should fix the slash problem lullis mentions. ([r prefix explained on SO](http://stackoverflow.com/questions/2241600/python-regex-r-prefix)) – j_syk Oct 12 '11 at 20:59

6 Answers6

3

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

Prescott
  • 7,312
  • 5
  • 49
  • 70
  • 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. – j_syk Oct 12 '11 at 21:12
  • Yes sorry, had to wait a day to mark as answer. I did have to start and restart – Prescott Oct 14 '11 at 05:44
  • How about a link for future users with a similar problem? – beerbajay Oct 17 '11 at 17:26
  • @beerbajay - aboslutely... http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html – Prescott Oct 18 '11 at 00:27
1
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

Danfi
  • 1,182
  • 3
  • 10
  • 14
1

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).

lullis
  • 304
  • 4
  • 7
0
Using the URLconf defined in mysite.urls, Django tried these
URL patterns, in this order:

This error message should list all possible URLs, including the 'expanded' urls from your pnasser app. Since you're only getting the URLs from your main urls.py, it suggests you haven't properly enabled the pnasser app in settings.py's INSTALLED_APPS.

beerbajay
  • 19,652
  • 6
  • 58
  • 75
  • It appears to be there - unless I've done something wrong here. I've edited my question to include the Installed_apps variable – Prescott Oct 11 '11 at 23:09
0

Within your URL's file you can write something like the following below. += obviously allows us to add additional 'patterns' to our 'urlpatterns' variable, this also means we can wrap these in an 'if' statement.

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

urlpatterns += patterns('',
    (r'^api/', include('api.urls')),
)

if settings.SHOP_TOGGLE:
    urlpatterns += patterns('',
        (r'^shop/', include('shop.urls')),)
    )
Chris Herring
  • 1,412
  • 9
  • 10
-1

The following works for me:

If your urls are not working correctly, you may need to add this line to location /:

fastcgi_split_path_info ^()(.*)$;

From: https://code.djangoproject.com/wiki/DjangoAndNginx