first_name和last_name不保存在自定义Django_Registration表单中 [英] first_name and last_name not saving in custom Django_Registration form

查看:173
本文介绍了first_name和last_name不保存在自定义Django_Registration表单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦用户提交了表单,我无法获取first_name和last_name变量。这是我的代码。我需要帮助。



Models.py



<$ p $来自django.db导入模型的$ p code从django.contrib.auth.models import

class MyRegistration(models.Model):
first_name = models.CharField(max_length = 100,null = True,unique = True)
last_name = models.CharField(max_length = 100,null = True,unique = True)

def __unicode __ )
return self.name

Forms.py / p>

从registration.forms导入RegistrationForm 
从django.forms import ModelForm
从模型导入MyRegistration
从django import forms

class MyRegistrationForm(forms.ModelForm):
class Meta:
model = MyRegistration


RegistrationForm.base_fields .update(MyRegistrationForm.base_fields)

class CustomRegistrationForm(RegistrationForm):
def save(self,profile_callback = None):
user = super(CustomRegistrationForm,self).save(profile_callback = None)
org,c = MyRegistration.objects.get_or_create(user = user,first_name =
self.cleaned_data ['first_name'],last_name = self.cleaned_data ['last_name'])

在根urls.py

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


解决方案

这是一个非常奇怪的方式,似乎有一些交叉的电线。首先,如果您要保存一些关于用户的额外数据,则不应通过注册模型来完成 - 您应该创建一个处理额外数据的配置文件应用程序。 django文档有一些关于如何关于这个



更重要的是,您可能创建的默认用户对象您的用户已经有 first_name last_name 的字段,因此您可以使用它们而不是创建新的配置文件模型


I am having trouble getting the first_name and last_name variables to save once the user has submitted the form. Here is my code.I need help.

Models.py

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

class MyRegistration(models.Model):
   first_name = models.CharField(max_length = 100, null = True, unique = True)
   last_name = models.CharField(max_length = 100, null = True, unique = True)

def __unicode__(self):
  return self.name

Forms.py

from registration.forms import RegistrationForm
from django.forms import ModelForm
from models import MyRegistration
from django import forms

class MyRegistrationForm(forms.ModelForm):
  class Meta:
    model = MyRegistration


RegistrationForm.base_fields.update(MyRegistrationForm.base_fields)

class CustomRegistrationForm(RegistrationForm):
   def save(self, profile_callback = None):
     user = super(CustomRegistrationForm, self).save(profile_callback = None)
     org, c = MyRegistration.objects.get_or_create(user=user, first_name =   
     self.cleaned_data['first_name'], last_name = self.cleaned_data['last_name'])

In root urls.py

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

解决方案

This is a very odd way to go about this and there seems to be some crossed wires. Firstly, if you are looking to save some extra data about users, it shouldn't be done via a registration model - you should create a profile application that handles extra data. The django docs have some good suggestions on how to go about this

More importantly, the default User object that you are more then likely creating for your users already has fields for first_name and last_name so you can use them instead of creating a new profile model

这篇关于first_name和last_name不保存在自定义Django_Registration表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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