i’ve a probelem with the request.user.is_authenticated()
this the view.
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from django.template import RequestContext
from forms import RegistrationForm
def ContributorRegistration(request):
if request.user.is_authenticated():
'''if user is logged in -> show profile'''
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
'''if post, check the data'''
form = ContributorRegistration(request.POST)
if form.is_valid():
''' if form is valid, save the data'''
user = User.objects.create_user(username=form.cleaned_data['username'],email = form.cleaned_data['email'], password= form.cleaned_data['password'])
user.save()
contributor = user.get_profile()
contributor.location = form.cleaned_data['location']
contributor.save()
return HttpResponseRedirect('profile.html')
else:
'''form not valid-> errors'''
return render_to_response('register.html',{'form':form},context_instance=RequestContext(request))
else:
'''method is not a post and user is not logged, show the registration form'''
form = RegistrationForm()
context={'form':form}
return render_to_response('register.html',context,context_instance=RequestContext(request))
basically,
if the user is logged in then the profile.html is shown: OK
if the user is not logged in and he’s not posting data then the form is shown: OK
when i submit the data from the form i receive back this error:
Request Method: POST
Request URL: http://localhost:8000/register/
Django Version: 1.4.1
Exception Type: AttributeError
Exception Value:
'QueryDict' object has no attribute 'user'
Exception Location: /Users/me/sw/DjangoProjects/earth/views.py in ContributorRegistration, line 9
where line 9 is if request.user.is_authenticated():
so seems that the request does not have the user object when submitting the form data.
how can i solve?
Thanks
You’re filling your own view function with the request.POST data as if it was the form.
Should be
In order to have access to
request.userobject, you need to have the User authentication middleware installed in your application. To do this (extremely easy), do the following:Go to your
settings.pyand add'django.contrib.auth'and'django.contrib.contenttypes'to theINSTALLED_APPStupple.You are very likely to require a syncdb command for it to be fully installed (you need some database tables for user authentication).
And that should make it work.