Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to map the main urls in a django project?

I've got a django project that contain some apps. The main urls.py includes the urls.py from the apps I've enabled, and all is good.

Now I want to configure the project so that when you go to http://testsite/, you'll get the same page that you get when you go to http://testsite/app/.

I can do this by duplicating the corresponding line in the apps urls.py in the projects urls.py, but this feels dirty.

Anyone know a better way?

like image 255
Alf Avatar asked Nov 26 '25 10:11

Alf


2 Answers

Set up a redirect_to from the first url to the second, ie:

from django.conf.urls.defaults import patterns, url, include
from django.views.generic.simple import redirect_to

    urlpatterns = patterns('',
        # Example:
        url(r'^$', redirect_to, {'url':'/app/'}),
        url(r'^app/', include('app.urls')),
        # ...
    )

HTH

like image 175
bruno desthuilliers Avatar answered Nov 28 '25 02:11

bruno desthuilliers


A redirect is the way to go, because you don't want multiple canonical URLs for the same resource (wastes Google juice). Ideally, you should do the redirect as close to the edge of your stack as possible to conserve server resources. So you can do a Django redirect_to urlconf entry, but you'd be better off with an Apache or nginx or insert-your-webserver-here redirect, so the initial request never has to hit Django at all.

like image 24
Carl Meyer Avatar answered Nov 28 '25 01:11

Carl Meyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!