سؤال

I've found an issue and tracked it down to url conf. I'm attempting to perform an ajax post to the /gallery/add page which adds a new record into the database.

Originally I added a urls.py into my app and then 'include'ed it from the root urls.py but that failed during the ajax post (appears /gallery/ is just returned from logging).

Then i reverted to just the root urls.py and it worked as i expected.

So the question is are these urlconfs equivalent?

(A)

# ./urls.py
from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^gallery$', 'gallery.views.home'),
    (r'^gallery/add$', 'gallery.views.add'), # ajax post works with this one
)

(B)

# ./urls.py
from django.conf.urls.defaults import *

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

# ./gallery/urls.py
from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'$', 'gallery.views.home'),
    (r'add$', 'gallery.views.add'), # ajax request doesn't work, instead it goes to gallery.views.home
)
هل كانت مفيدة؟

المحلول

In the second example you still need the ^ because otherwise the first regex will just match any old string that has an ending (due to the $), and that is of course all of them :)

# ./gallery/urls.py
from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^$', 'gallery.views.home'),
    (r'^add$', 'gallery.views.add'),
)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top