Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

python - django-registration-redux add extra field

7 and python 2.7. i want to add extra field in django registration. i try to extend with my model like this:

class Seller(models.Model):
user            = models.ForeignKey(User, unique=True)
name            = models.CharField(max_length=25)
phone_number    = models.BigIntegerField()
email           = models.EmailField(max_length=75)

def __str__(self):
    return self.name;

and i add form.py like this

    from django import forms
    from registration.forms import RegistrationForm
    from django.forms import ModelForm
    from django.contrib.auth.models import User
    from kerajinan.models import Product, Category, Seller

    class SellerForm(forms.ModelsForm):
        class Meta:
            model   = Seller
            fields  = ('name','phone_number','email')

and modify url.py like this:

url(r'^accounts/', 'registration.views.register',{'form_class':SellerForm,'backend': 'registration.backends.default.DefaultBackend'})

how to use those model with django registration and i get error syntax with my ulr.py?

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's works for me:
models.py

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    field = models.CharField(max_length=3)
    user = models.OneToOneField(User)

forms.py

from registration.forms import RegistrationFormUniqueEmail
from django import forms

class UserProfileRegistrationForm(RegistrationFormUniqueEmail):
    field = forms.CharField()

Create regbackend.py and write:

from registration.backends.default.views import RegistrationView
from forms import UserProfileRegistrationForm
from models import UserProfile

class MyRegistrationView(RegistrationView):

    form_class = UserProfileRegistrationForm

    def register(self, request, form_class):
        new_user = super(MyRegistrationView, self).register(request, form_class)
        user_profile = UserProfile()
        user_profile.user = new_user
        user_profile.field = form_class.cleaned_data['field']
        user_profile.save()
        return user_profile

And urls.py

from django.conf.urls import include, url
import regbackend

urlpatterns = [
url(r'^accounts/register/$', regbackend.MyRegistrationView.as_view(), name='registration_register'),
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...