4
votes

I've created custom user registration form for django, but it's not working :-(

I've created LOGINSYS app to manipulate Users (Login, Profile page, Registration)

My forms.py looks like this:


    #-*- coding:utf-8 -*-

    from django import forms
    from django.contrib.auth.models import User
    from django.contrib.auth.forms import UserCreationForm
    from datetime import date, timedelta

    class MyRegistrationForm(UserCreationForm):

        def get_image_path(self, filename):
            path = ''.join([date.today().strftime('../static/user_image/%Y/%m/%d/'), translit.slugify(filename), ".jpg"])
            return path

        first_name = forms.CharField (required = True)
        last_name = forms.CharField (required = True)
        telephone = forms.CharField (required = True)
        email = forms.EmailField (required = False)
        #user_image = forms.ImageField(path = get_image_path, required = False, allow_empty_file = True )


        class Meta:
            model = User
            fields = ('username', 'password1', 'password2', 'first_name', 'last_name', 'telephone', 'email')

        def save (self, commit=True):
            user = super(UserCreationForm, self).save(commit=False)
            user.first_name = self.cleaned_data['first_name']
            user.last_name = self.cleaned_data['last_name']
            user.telephone = self.cleaned_data['telephone']
            user.email = self.cleaned_data['email']
            user.set_password(self.cleaned_data["password1"])
            #user.user_image = self.cleaned_data['user_image']
            if commit:
                user.save()

my views.py looks like:


    #-*- coding:utf-8 -*-

    from django.shortcuts import render, render_to_response, redirect
    from django.http.response import HttpResponse
    from django.http import HttpResponseRedirect
    from django.template.loader import get_template
    from django.template import Context
    from django.template import RequestContext
    from advert.models import Advert, AdvertCategory, AdvertSection
    from django.core.exceptions import ObjectDoesNotExist
    from django.core.context_processors import csrf
    from django.core.urlresolvers import reverse
    from django.core.paginator import Paginator
    from datetime import date, timedelta
    from PIL import Image
    from django.conf import settings
    from django.contrib.syndication.views import Feed
    from django.contrib import auth
    from django.contrib.auth.forms import UserCreationForm
    from loginsys.forms import MyRegistrationForm

    def register(request):
        args = {}
        args.update(csrf(request))
        args['form'] = MyRegistrationForm()
        if request.POST:
            newuser_form = MyRegistrationForm(request.POST)
            if newuser_form.is_valid():
                username = newuser_form.cleaned_data['username']
                password = newuser_form.cleaned_data['password1']
                #password2 = newuser_form.cleaned_data['password2']
                first_name = newuser_form.cleaned_data['first_name']
                last_name = newuser_form.cleaned_data['last_name']
                telephone = newuser_form.cleaned_data['telephone']
                email = newuser_form.cleaned_data['email']
                newuser_form.save()
                newuser = auth.authenticate(username=newuser_form.cleaned_data['username'], password=newuser_form.cleaned_data['password2'])
                auth.login(request, newuser)
                return redirect('/')
            else:
                args['reg_form'] = newuser_form
        return redirect('/')

Unfortunately when i register on front-end it doesn't do anything :-(

Please help me

2
So you get any error ? Can you tell... How you define telephone in models.py file ?Raja Simon
I did not define it in models.py since I use default django User model. So i just created telephone column in auth_user tableilyas Jumadurdyew
Try printing newuser_form.is_valid() to see if it is really True. You are redirected to / in both cases so yo can differentiate them.Domen Blenkuš
You might need to do if request.method == 'POST': instead of if request.POST:jape
You can also shorten this line: newuser = auth.authenticate(username=newuser_form.cleaned_data['username'], password=newuser_form.cleaned_data['password2']) to -> newuser = auth.authenticate(username=username, password=password)jape

2 Answers

0
votes

Your code does not render the form, see the usage of render in this example.

I recommend using django-auth-tools for building your own custom user model. It supplies basic models, views and forms which can be easily extended.

0
votes

When you override the method save using 'super', you have to use the MyRegistrationForm, but not(!) superclass UserCreationForm.

Rewrite class method accordingly:

class MyRegistrationForm(UserCreationForm):
    ...
    def save (self, commit=True):
        user = super(MyRegistrationForm, self).save(commit=False)
        ...