first_name and last_name not saving in custom Django_Registration form

半腔热情 提交于 2019-12-13 02:03:40

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/9057856/first-name-and-last-name-not-saving-in-custom-django-registration-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!