EsseTi
EsseTi

Reputation: 4301

Django request.user.is_authenticated() not working when sending form data

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

Upvotes: 0

Views: 2771

Answers (2)

Abid A
Abid A

Reputation: 7858

Is it just me or is the name of your form the same as your view function ContributorRegistration?

Perhaps you've made a typo.

Upvotes: 0

Mamsaac
Mamsaac

Reputation: 6273

You're filling your own view function with the request.POST data as if it was the form.

if request.method == 'POST':
    '''if post, check the data'''
    form = ContributorRegistration(request.POST)
    if form.is_valid():

Should be

if request.method == 'POST':
    '''if post, check the data'''
    form = RegistrationForm(request.POST)
    if form.is_valid():

In order to have access to request.user object, you need to have the User authentication middleware installed in your application. To do this (extremely easy), do the following:

Go to your settings.py and add 'django.contrib.auth' and 'django.contrib.contenttypes' to the INSTALLED_APPS tupple.

You are very likely to require a syncdb command for it to be fully installed (you need some database tables for user authentication).

python manage.py syncdb

And that should make it work.

Upvotes: 3

Related Questions