forms - How to add custom User model to Registrationform in Django? -
i have done lot of research these days dont how done. have basic user model , profile model shown below:
class profile(models.model): user = models.onetoonefield(user, null=true) address= models.charfield(max_length=100) posts = models.integerfield(max_length=100) @receiver(post_save, sender=user) def create_user_profile(sender, instance, created, **kwargs): if created: profile.objects.create(user=instance) @receiver(post_save, sender=user) def save_user_profile(sender, instance, **kwargs): instance.profile.save()
my forms.py
class registrationform(usercreationform): class meta: model = user fields = {'username','email', 'password1', 'password2', 'first_name', 'last_name'} class userprofileform(forms.modelform): class meta: model = profile
views.py
def register(request): if request.method == 'post': form = registrationform(request.post) if form.is_valid(): user = form.save(commit=false) username = form.cleaned_data['username'] user.save() user = authenticate(username=username) return redirect('/home') else: form = registrationform() args = {'form' : form} return render(request, 'home/reg_form.html', args)
so what's missing in code make appear on registration form?
thanks in advance
this way did it.
views.py:
def register_user(request): if request.method == 'post': register = registerform(request.post, prefix='register') usrprofile = profileform(request.post, prefix='profile') if register.is_valid() * usrprofile.is_valid(): user = register.save() usrprof = usrprofile.save(commit=false) usrprof.user = user usrprof.set_token() usrprof.subscribed = '1' usrprof.save() return httpresponse('congrats') else: return httpresponse('errors') else: userform = registerform(prefix='register') userprofileform = profileform(prefix='profile') return render(request, 'registration/register_form.html', {'userform': userform, 'userprofileform': userprofileform})
template:
<form action="" method="post"> <div class="field"> {{ userform.first_name.errors }} <label for="id_first_name">first name: </label> {{ userform.first_name }} </div> <div class="field"> {{ userform.last_name.errors }} <label for="id_last_name">last name: </label> {{ userform.last_name }} </div> <div class="field"> {{ userform.email.errors }} <label for="id_email">email: </label> {{ userform.email }} </div> <div class="field"> {{ userform.username.errors }} <label for="id_username">username: </label> {{ userform.username }} </div> <div class="field"> {{ userform.password.errors }} <label for="id_password1">password: </label> {{ userform.password }} </div> <div class="field"> {{ userprofileform.address.errors }} <label for="id_address">address: </label> {{ userprofileform.address }} </div> <div class="field"> {{ userprofileform.city.errors }} <label for="id_city">city: </label> {{ userprofileform.city }} </div> <div class="field"> {{ userprofileform.post.errors }} <label for="id_post">post: </label> {{ userprofileform.post }} </div> {% csrf_token %} <input type="submit" value="submit"> </form>
models.py (i used built in user model):
from django.db import models random import choice django.utils.translation import ugettext_lazy _ django.contrib.auth.models import user class profile(models.model): user = models.onetoonefield(user) address = models.charfield(_('address'), max_length=50, blank=true, null=true) city = models.charfield(_('city'), max_length=40, blank=true, null=true) post = models.charfield(_('last name'), max_length=40, blank=true, null=true) token = models.charfield(_('token'), max_length=15, unique=true, db_index=true, null=true) subscribed = models.charfield(_('subscribed or not'), max_length=2, blank=true, null=true) def set_token(self): self.token = ''.join([choice('abcdefghijklmnopqrstuvwxyz0123456789') in range(15)]) def save(self, *args, **kwargs): super(profile, self).save(*args, **kwargs) self.set_token()
forms.py:
from django import forms django.contrib.auth.models import user getall.models import profile django.utils.translation import ugettext_lazy _ class registerform(forms.modelform): password = forms.charfield(widget=forms.passwordinput()) class meta: model = user fields = ['email', 'first_name', 'last_name', 'password', 'username'] class profileform(forms.modelform): class meta: model = profile fields = ['address', 'city', 'post']
Comments
Post a Comment