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',
)
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With