phicon
phicon

Reputation: 3617

Use dynamic alias in Python Django when switching from Debug to Production

I have the following http address when running in development (manage.py runserver)

http://127.0.0.1:8000/

When using wamp i use an alias named picon

http://localhost/picon/

When clicking on links from the nav bar the navigation works fine for both deployments. But when i click from the following views.py the urls break;

def addcustomer(request):      
    form = AddCustomerForm(request.POST or None)

    if form.is_valid():
        save_it = form.save(commit=False)
        save_it.save()
        messages.success(request, 'Customer added succesfully')
        return HttpResponseRedirect('/picon/customers')

    return render_to_response("addcustomer.html",
                              locals(),
                              context_instance=RequestContext(request))

In the production server i use the prefix /picon/ because otherwise the link can not be found when using an alias. Off course this alias is not available on the development version.

So my question, can i create a dynamic alias that has a relation to the debug status in the settings.py file.

for example;

If Debug:
    alias = ''
else:
    alias = 'picon/'

if this is possible, how would i then reference to the alias in the views.py using the following line;

return HttpResponseRedirect('/picon/customers') 

Edit: added my urls.py

from django.conf.urls import patterns, include, url

from django.conf import settings
from django.conf.urls.static import static

from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', 'signups.views.home', name='home'),

    url(r'^thank-you/$', 'signups.views.thankyou', name='thankyou'),
    url(r'^about-us/$', 'signups.views.aboutus', name='aboutus'),

    url(r'^customers/$', 'formlist.views.customers', name='customers'),
    url(r'^addcustomer/$', 'formlist.views.addcustomer', name='addcustomer'),

    #url(r'^addcustomer_res/$', 'formlist.views.addcustomer', name='addcustomer'),

    url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

Any suggestions are much appreciated.

Regards.

Upvotes: 0

Views: 341

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599628

This is exactly why you should not hard-code URLs in views or templates. Use django.core.urlresolvers.reverse - or the {% url %} template tag - to calculate the URL dynamically, including any prefix.

return HttpResponseRedirect(reverse('customers'))

where 'customers' is the name value from the URLconf.

Upvotes: 3

Luis Masuelli
Luis Masuelli

Reputation: 12333

You can include the urls from another file, dynamically:

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin

#PAY ATTENTION HERE
from another_location import urls as another_urls

admin.autodiscover()

if settings.DEBUG:
    urlpatterns = url(r'^picon/', include(another_urls))
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)
else:
    urlpatterns = url(r'^', include(another_urls))

Having your another_location.urls file like this:

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', 'signups.views.home', name='home'),

    url(r'^thank-you/$', 'signups.views.thankyou', name='thankyou'),
    url(r'^about-us/$', 'signups.views.aboutus', name='aboutus'),

    url(r'^customers/$', 'formlist.views.customers', name='customers'),
    url(r'^addcustomer/$', 'formlist.views.addcustomer', name='addcustomer'),

    #url(r'^addcustomer_res/$', 'formlist.views.addcustomer', name='addcustomer'),
    url(r'^admin/', include(admin.site.urls)),
)

You must ensure such file is pythonpath-reachable via imports.

Edit: also, don't hardcode URLS. Always use reverse. It will be harmless to you since you gave names for each of your urls (reverse('customers') will give you the right url no matter where is it deployed).

Upvotes: 1

Related Questions