djangohttpresponse.redirecthttp-response-codesdjango-flatpages

Why does django return 301 and 302 as server response codes after a user logs in and a flatpage is displayed?


I'm creating a django app. Users login and are shown a static web page that is managed by the flatpages app.

Here are typical status messages from the dev server:

 [15/Aug/2013 18:43:16] "GET / HTTP/1.1" 200 1263
 [15/Aug/2013 18:43:23] "POST / HTTP/1.1" 302 0
 [15/Aug/2013 18:43:23] "GET /home HTTP/1.1" 301 0
 [15/Aug/2013 18:43:23] "GET /home/ HTTP/1.1" 200 4529

Why does the server respond with 302 for a put request?

What is causing the third line? Why is this message sent out at all? Shouldn't this be something that is caught by the flatpages middleware? Is my web client sending the request underling the fourth line? How does it know to do this?

I guess the most important question is: Am I doing something wrong?

Thanks for the help!

urls.py

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'django.contrib.auth.views.login'),
    url(r'^logout$', 'guide.views.logout_view'),
    # other patterns
    (r'', include('django.contrib.flatpages.urls')),
)

views.py

def home(request):
    if request.user.is_authenticated() == False:
        return HttpResponseRedirect('/')
    return HttpResponseRedirect('/home/')

Excerpt from settings.py

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    'guide.middleware.LogActivity'
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.flatpages',
    'django.contrib.admin',
    'guide',
)

Solution

  • I can't see your url pattern for the home view. But it's probably the missing slash which makes django send out a auto-redirect:

    https://docs.djangoproject.com/en/dev/ref/settings/#append-slash

    Is my web client sending the request underling the fourth line? How does it know to do this?

    Yes, Status code 301 in line 3 tells the browser 'the page you requested moved to another url x'. And browsers will usually always automatically send a new request to that new url x which is line 4.