Expanding UserCreationForm for customized Django registration -
i trying understand how expand creationform library utilize custom fields have in model. basically, in model, have field called codeid , goal have user type in integer , have store database register. know i'm on right track, there flawed logic behind current code.
forms.py
from django import forms django.contrib.auth.models import user # fill in custom user info save django.contrib.auth.forms import usercreationform .models import address, job class myregistrationform(usercreationform): username = forms.charfield(required = true) #udid = forms.charfield(required = true) class meta: model = address fields = ('username', 'codeid', ) def save(self,commit = true): user = super(myregistrationform, self).save(commit = false) user.codeid = self.cleaned_data['codeid'] if commit: user.save() return
views.py
from django.shortcuts import render django.http import httpresponseredirect django.contrib import auth django.core.context_processors import csrf forms import myregistrationform def register_user(request): if request.method == 'post': form = myregistrationform(request.post) # create form object if form.is_valid(): form.save() return httpresponseredirect('/') args = {} args.update(csrf(request)) args['form'] = myregistrationform() print args return render(request, 'register.html', args)
models.py
from django.db import models django.contrib.auth.models import user class address(models.model): user = models.foreignkey(user) street_address = models.charfield(max_length = 200) city = models.charfield(max_length = 100) state = models.charfield(max_length = 100) zipcode = models.integerfield(max_length = 5) updated = models.datetimefield(auto_now = true, auto_now_add = false) timestamp = models.datetimefield(auto_now = false, auto_now_add = true) active = models.booleanfield(default = true) codeid = models.integerfield(max_length = 10, default = 0) image = models.imagefield(upload_to='profiles/', default="") def __str__(self,): return 'test'
registration html
{% extends 'home.html' %} {%block content %} <h2>register</h2> <form action='/register/' method = "post"> {% csrf_token %} {{ form }} <input type = "submit" value = "register" /> </form> {% endblock %}
make change in forms.py :
return user
Comments
Post a Comment