Question

I want to redirect from / to /post/, but in a trial test, I created a circular reference of some sorts, and instead of going to /post/ and then showing my post index, it goes to /post/post/post/.... In this test, I did urls-including too, so here's my two urls file.

Project urls.py:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls), name='admin'),
    url(r'^post/', include('post.urls'), name='post'),
    url(r'^/$', RedirectView.as_view(url='index'))
)

Application Post urls.py:

urlpatterns = patterns(
    '',  # prefix
    url(r'^update/(?P<id>[1-9][0-9]*)', PostUpdateView.as_view(), name='post_update'),
    url(r'^delete/(?P<id>[1-9][0-9]*)', PostDeleteView.as_view(), name='post_delete'),
    url(r'^detail/(?P<id>[1-9][0-9]*)', PostDetailView.as_view(), name='post_detail'),
    url(r'^create/$', PostCreateView.as_view(), name='post_create'),
    url(r'^list/$', PostListView.as_view(), name='post_list'),
    url(r'^$', PostIndexView.as_view(), name='post_index')
)

UPDATE:

The problem was that Firefox 26 (and possibly other browsers?) was caching 301 (default redirect code used by Django's RedirectView?) HTTP responses, as explained in this StackOverflow post.

In order to solve this, you have to clear your cache (and maybe site preferences) and always add permanent=False to your redirects, so it uses 302 (non-cacheable) instead of 301 (cacheable) responses. You may change this to it's default behaviour when you are dead-fixed on using that redirect (ie, deploying to production enviorment).

Was it helpful?

Solution

Use this instead in your project urls.py. It'll basically just do a reverse on the name and use that url. Docs

url(r'^$', RedirectView.as_view(pattern_name='post_index'))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top