views:

148

answers:

1

I am trying to refactor out my application a little bit to keep it from getting too unwieldily. So I started to move some of the urlpatterns out to sub files as the documentation proposes.

Besides that fact that it just doesn't seem to be working (the items are not being rerouted) but when I go to the admin, it says that 'urlpatterns has not been defined'.

The urls.py I have at the root of my application is:

if settings.ENABLE_SSL:
urlpatterns = patterns('',
    (r'^checkout/orderform/onepage/(\w*)/$','checkout.views.one_page_orderform',{'SSL':True},'commerce.checkout.views.single_product_orderform'),
)
else:
    urlpatterns = patterns('',
    (r'^checkout/orderform/onepage/(\w*)/$','commerce.checkout.views.single_product_orderform'),
)

urlpatterns+= patterns('',
    (r'^$', 'alchemysites.views.route_to_home'),
    (r'^%s/' % settings.DAJAXICE_MEDIA_PREFIX, include('dajaxice.urls')),
    (r'^/checkout/', include('commerce.urls')),
    (r'^/offers',include('commerce.urls')),
    (r'^/order/',include('commerce.urls')),
    (r'^admin/', include(admin.site.urls)),
    (r'^accounts/login/$',  login),
    (r'^accounts/logout/$', logout),
    (r'^(?P<path>.*)/$','alchemysites.views.get_path'),
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.MEDIA_ROOT}),

The urls I have moved out so far are the checkout/offers/order which are all subapps of 'commerce' where the urls.py for the apps are so to be clear.

/urls.py in questions (included here)

/commerce/urls.py where the urls.py I want to include is:

order_info = {
'queryset': Order.objects.all(),
}


urlpatterns+= patterns('',


(r'^offers/$','offers.views.start_offers'),
(r'^offers/([a-zA-Z0-9-]*)/order/(\d*)/add/([a-zA-Z0-9-]*)/(\w*)/next/([a-zA-Z0-9-)/$','offers.views.show_offer'),
(r'^reports/orders/$', list_detail.object_list,order_info),
)

and the applications offers lies under commerce.

And so the additional problem is that admin will not work at all, so I'm thinking because I killed it somewhere with my includes.

Things I have checked for: Is the urlpatterns variable accidentally getting reset somewhere (i.e. urlpatterns = patterns, instead of urlpatterns+= patterns) Are the patterns in commerce.urls valid (yes, when moved back to root they work).

So from there I am stumped. I can move everything back into the root, but was trying to get a little decoupled, not just for theoretical reason but for some short terms ones.

Lastly if I enter www.domainname/checkout/orderform/onepage/xxxjsd I get the correct page. However, entering www.domainname/checkout/ gets handled by the alchemysites.views.get_path.

If not the answer (because this is pretty darn specific), then is there a good way for troubleshoot urls.py? It seems to just be trial and error. Seems there should be some sort of parser that will tell you what your urlpatterns will do.

+2  A: 

Have a look at the django docs for including other url confs. I think you might have misunderstood them. In particular

Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

As an example, you have

(r'^/checkout/', include('commerce.urls')),
(r'^/offers',include('commerce.urls')),
(r'^/order/',include('commerce.urls')),

This means that

/checkout/offers/
/offers/offers/
/order/offers/

will all match the url pattern (r'^offers/$','offers.views.start_offers') in commerce/urls.py.

If you want to define a view for /checkout/ in commerce.py, you need to add the pattern

(r'^$', 'path_to_your_view')

because the /checkout/ part will be chopped off by the include()


As, an aside:

In /commerce/urls.py, use

urlpatterns = patterns('',
    ...

for the first patterns you define. You can then use urlpatterns += later in the same file.

Alasdair
Yes, that was exactly it. I moved the sections even deeper to commerce.checkout.urls, etc. And yes I did misunderstand the part about "chopping off", which I thought only referred to the trailing slash. Thanks
zenWeasel