Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
  • The first line is for the login page at /. This is served successfully, code 200.
  • The second line is the form input. The server response code is 302, which means the page is moved temporarily.
  • The third line is is an attempt to retrieve a page ('/home') that doesn't exist, because the underlying page is served by flatpages. The 301 server response code indicates that the page has been moved permanently.
  • The fourth line is a successful delivery of content ('/home') from flatpages.

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',
)
like image 843
benevolentprof Avatar asked Aug 16 '13 02:08

benevolentprof


3 Answers

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.

like image 107
djangonaut Avatar answered Nov 17 '22 07:11

djangonaut


I can't comment or upvote, but wanted to add for others that beluga.me at https://stackoverflow.com/a/18265990/4651336 was spot on and I was missing a trailing slash after my success_url.

This:

success_url = 'step-two'

changed to:

success_url = 'step-two/'

fixed it.

like image 20
M. Mills Avatar answered Nov 17 '22 09:11

M. Mills


Based on user640916's hint, here's how I cleaned up the errors.

To urls.py, I added:

url(r'^home$', 'guide.views.home'),

To views.py, I added:

from django.contrib.flatpages.views import flatpage

def home(request):
    return flatpage(request, "/home/")

My server status messages for login now look like:

[17/Aug/2013 09:13:52] "GET / HTTP/1.1" 200 1263
[17/Aug/2013 09:14:00] "POST / HTTP/1.1" 302 0
[17/Aug/2013 09:14:00] "GET /home HTTP/1.1" 200 4529

Not exactly what I was looking for, but it works. I still have the feeling that I'm not doing something right. It appears that django.contrib.auth automatically looks for a home view at the url "/home" without the trailing slash.

like image 23
benevolentprof Avatar answered Nov 17 '22 07:11

benevolentprof