注册表格与个人资料的字段 [英] Registration form with profile's field

查看:91
本文介绍了注册表格与个人资料的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题。这是我的个人资料:



class Profile(models.Model):

  user = models.ForeignKey(User,unique = True)
born = models.DateTimeField('born to')
photo = models.ImageField(upload_to ='profile_photo')

我想用这个字段创建一个注册表格(来自用户和个人资料模型):



用户名,first_name,last_name,出生和照片。



所有这些字段都是必需的。



如何做?



get_profile()在这个问题的模板中如何工作?



谢谢:)

解决方案

设置



您是否使用 django-profiles django-registration 项目?如果没有,你应该使用这些代码,大部分的代码都是为你写的。



个人资料



您的用户个人资料代码是:

  class Profile(models.Model):
user = models $($)
born = models.DateTimeField('born to')
photo = models.ImageField(upload_to ='profile_photo')

您是否在Django设置中正确设置了该配置文件?如果没有,您应该添加,如果没有,请将 yourapp 替换为您的应用名称:

  AUTH_PROFILE_MODULE =yourapp.Profile

注册表单



django-registration 附带一些默认注册表单,但您指定要创建自己的。 Django表单字段默认为必需,因此您不需要改变。重要的部分是确保处理现有的注册表单字段并添加配置文件创建。这样做应该可以工作:

从$ d code从django导入表单
from registration.forms import RegistrationForm
from yourapp .models import Profile
from registration.models import RegistrationProfile

class YourRegistrationForm(RegistrationForm):
born = forms.DateTimeField()
photo = forms.ImageField()

def save(self,profile_callback = None)
new_user = RegistrationProfile.objects.create_inactive_user(username = self.cleaned_data ['username'],
password = self.cleaned_data ['password1'],
email = self.cleaned_data ['email'])
new_profile =个人资料(user = new_user,born = self.cleaned_data ['born'],photo = self.cleaned_data [ 'photo'])
new_profile.save()
return new_user

将其合并



您可以使用默认的 django-registration 模板和视图,但是想要在 urls.py 中传递您的表单:

 从registration.backends.default import DefaultBackend 
from registration.views import activate
from registration.views import register

#...其余的urls直到你找到某处要添加...

url(r'^ register / $',register,
{'form_class':YourRegistrationForm,'backend':DefaultBackend},
name = 'registration_register'),


I have a simply question. This is my profile:

class Profile(models.Model):

user = models.ForeignKey(User, unique=True)
born = models.DateTimeField('born to')    
photo = models.ImageField(upload_to='profile_photo')

I want to create a REGISTRATION FORM with this fields ( from User and Profile models ):

username, first_name, last_name, born and photo.

All this fields are required.

How do I do that ??

How does get_profile() work in a template for this issue ?

Thank you :)

解决方案

Setup

Are you using the django-profiles and django-registration projects? If not, you should—much of this code has already been written for you.

Profile

Your user profile code is:

class Profile(models.Model):
    user = models.ForeignKey(User, unique=True)
    born = models.DateTimeField('born to')    
    photo = models.ImageField(upload_to='profile_photo')

Have you correctly setup this profile in your Django settings? You should add this if not, substituting yourapp for your app's name:

AUTH_PROFILE_MODULE = "yourapp.Profile"

Registration Form

django-registration comes with some default registration forms but you specified you wanted to create your own. Each Django form field defaults to required so you should not need to change that. The important part is just making sure to handle the existing registration form fields and adding in the profile creation. Something like this should work:

from django import forms
from registration.forms import RegistrationForm
from yourapp.models import Profile
from registration.models import RegistrationProfile

class YourRegistrationForm(RegistrationForm):
    born = forms.DateTimeField()
    photo = forms.ImageField()

    def save(self, profile_callback=None):
        new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
        password=self.cleaned_data['password1'],
        email = self.cleaned_data['email'])
        new_profile = Profile(user=new_user, born=self.cleaned_data['born'], photo=self.cleaned_data['photo'])
        new_profile.save()
        return new_user

Bringing it together

You can use the default django-registration templates and views, but will want to pass them your form in urls.py:

from registration.backends.default import DefaultBackend
from registration.views import activate
from registration.views import register

# ... the rest of your urls until you find somewhere you want to add ...

url(r'^register/$', register,
    {'form_class' : YourRegistrationForm, 'backend': DefaultBackend},
    name='registration_register'),

这篇关于注册表格与个人资料的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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