1

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).

Community
  • 1
  • 1
Alxe
  • 381
  • 3
  • 12

1 Answers1

1

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'))
schillingt
  • 13,493
  • 2
  • 32
  • 34
  • I changed the `/post/` to `/post/index` in my Post `urls.py` and Django still redirects to `/post/post/post/post/...` So the problem is probably with the RedirectView – Alxe May 09 '14 at 12:12
  • Are you redirecting to '/' anywhere in ```PostIndexView```? – schillingt May 09 '14 at 12:13
  • No, and the newly-changed `/post/index` properly renders the view. – Alxe May 09 '14 at 13:13
  • @Alxe Try changing it from ```^/$``` to ```^$```. I've edited my answer. – schillingt May 09 '14 at 13:46
  • Somehow, somewhere, somewhy, either my DB and/or my browser got brain damaged from that. I deleted the sqlite and firefox's cache/site preferences/sessions and it works now! – Alxe May 09 '14 at 14:35
  • After some more trial, yes, it was Firefox fault. Do you believe this may be specific for Firefox or just that browser don't like redirections? (caching redirections, in this case) – Alxe May 09 '14 at 14:37
  • @alxe: I'm not sure, but it might have been possible if you set ```permanent``` to True. https://docs.djangoproject.com/en/dev/ref/class-based-views/base/#django.views.generic.base.RedirectView.permanent – schillingt May 09 '14 at 14:59
  • Permanent is True by default, as I learned after replying, hence the post edit. Thanks for the help. :-) – Alxe May 09 '14 at 17:43