django-registration-redux添加额外的字段 [英] django-registration-redux add extra field

查看:151
本文介绍了django-registration-redux添加额外的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

7和python 2.7。我想在django注册中添加额外的字段。我试图用我的模型来扩展:

  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;

我添加form.py这样

  from django import forms 
from registration.forms import RegistrationForm
from django.forms import ModelForm
from django.contrib.auth.models import User
来自kerajinan.models import产品,类别,卖家

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

并修改url.py,如下所示: / p>

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

如何使用这些模型与django注册和我得到我的ulr.py的错误语法?



谢谢

解决方案

它适用于我:

models.py

 来自django.contrib.auth.models import的
用户

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

forms.py

  from registration.forms import RegistrationFormUniqueEmail 
从django导入表单

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

创建regbackend.py并写入:

  from registration.backends.default.views import RegistrationView 
从表单导入UserProfileRegistrationForm
从模型import UserProfile

class MyRegistrationView(RegistrationView):

form_class = UserProfileRegistrationForm

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

和urls.py

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

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


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

解决方案

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'),
]

这篇关于django-registration-redux添加额外的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆