• <bdo id='q2UXm'></bdo><ul id='q2UXm'></ul>

      <legend id='q2UXm'><style id='q2UXm'><dir id='q2UXm'><q id='q2UXm'></q></dir></style></legend>
      1. <tfoot id='q2UXm'></tfoot>
      2. <i id='q2UXm'><tr id='q2UXm'><dt id='q2UXm'><q id='q2UXm'><span id='q2UXm'><b id='q2UXm'><form id='q2UXm'><ins id='q2UXm'></ins><ul id='q2UXm'></ul><sub id='q2UXm'></sub></form><legend id='q2UXm'></legend><bdo id='q2UXm'><pre id='q2UXm'><center id='q2UXm'></center></pre></bdo></b><th id='q2UXm'></th></span></q></dt></tr></i><div id='q2UXm'><tfoot id='q2UXm'></tfoot><dl id='q2UXm'><fieldset id='q2UXm'></fieldset></dl></div>

        <small id='q2UXm'></small><noframes id='q2UXm'>

        Django-注册&amp;Django-Profile,使用您自己的自定义表单

        Django-Registration amp; Django-Profile, using your own custom form(Django-注册amp;Django-Profile,使用您自己的自定义表单)
        • <legend id='WE7Ec'><style id='WE7Ec'><dir id='WE7Ec'><q id='WE7Ec'></q></dir></style></legend>

          1. <i id='WE7Ec'><tr id='WE7Ec'><dt id='WE7Ec'><q id='WE7Ec'><span id='WE7Ec'><b id='WE7Ec'><form id='WE7Ec'><ins id='WE7Ec'></ins><ul id='WE7Ec'></ul><sub id='WE7Ec'></sub></form><legend id='WE7Ec'></legend><bdo id='WE7Ec'><pre id='WE7Ec'><center id='WE7Ec'></center></pre></bdo></b><th id='WE7Ec'></th></span></q></dt></tr></i><div id='WE7Ec'><tfoot id='WE7Ec'></tfoot><dl id='WE7Ec'><fieldset id='WE7Ec'></fieldset></dl></div>

            <small id='WE7Ec'></small><noframes id='WE7Ec'>

                <bdo id='WE7Ec'></bdo><ul id='WE7Ec'></ul>
                    <tbody id='WE7Ec'></tbody>

                1. <tfoot id='WE7Ec'></tfoot>
                  本文介绍了Django-注册&amp;Django-Profile,使用您自己的自定义表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 django-registration 和 django-profile 来处理注册和配置文件.我想在注册时为用户创建个人资料.我创建了一个自定义注册表单,并使用以下教程将其添加到 urls.py:

                  I am making use of django-registration and django-profile to handle registration and profiles. I would like to create a profile for the user at the time of registration. I have created a custom registration form, and added that to the urls.py using the tutorial on:

                  http://dewful.com/?p=70

                  教程中的基本思想是覆盖默认注册表单以同时创建配置文件.

                  The basic idea in the tutorial is to override the default registration form to create the profile at the same time.

                  forms.py - 在我的个人资料应用中

                  forms.py - In my profiles app

                  from django import forms
                  from registration.forms import RegistrationForm
                  from django.utils.translation import ugettext_lazy as _
                  from profiles.models import UserProfile
                  from registration.models import RegistrationProfile
                  
                  attrs_dict = { 'class': 'required' }
                  
                  class UserRegistrationForm(RegistrationForm):
                      city = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
                  
                      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 = UserProfile(user=new_user, city=self.cleaned_data['city'])
                          new_profile.save()
                          return new_user
                  

                  在 urls.py 中

                  In urls.py

                  from profiles.forms import UserRegistrationForm
                  

                  url(r'^register/$',
                                             register,
                                             {'backend': 'registration.backends.default.DefaultBackend', 'form_class' : UserRegistrationForm},
                                             name='registration_register'),
                  

                  显示表格,我可以在城市中输入,但它不会在数据库中保存或创建条目.

                  The form is displayed, and i can enter in City, however it does not save or create the entry in the DB.

                  推荐答案

                  您已经成功完成了一半 - 您已经成功构建了一个替换默认表单的自定义表单.但是您正在尝试使用模型表单上的 save() 方法进行自定义处理.这在旧版本的 django-registration 中是可能的,但我可以从您在 URL conf 中指定后端的事实中看出,您使用的是 v0.8.

                  You're halfway there - you've successfully built a custom form that replaces the default form. But you're attempting to do your custom processing with a save() method on your model form. That was possible in older versions of django-registration, but I can see from the fact that you specified a backend in your URL conf that you're using v0.8.

                  升级指南说:

                  以前,用于收集的表单注册期间的数据是预期的实现一个 save() 方法将创建新的用户帐户.这已不再是这种情况;创造该帐户由后端处理,所以任何自定义逻辑都应该是移动到自定义后端,或通过将侦听器连接到信号在注册过程中发送.

                  Previously, the form used to collect data during registration was expected to implement a save() method which would create the new user account. This is no longer the case; creating the account is handled by the backend, and so any custom logic should be moved into a custom backend, or by connecting listeners to the signals sent during the registration process.

                  换句话说,现在您使用的是 0.8 版,表单上的 save() 方法将被忽略.您需要使用自定义后端或信号进行自定义处理.我选择创建一个自定义后端(如果有人使用信号进行此操作,请发布代码 - 我无法让它以这种方式工作).您应该能够修改它以保存到您的自定义配置文件中.

                  In other words, the save() method on the form is being ignored now that you're on version 0.8. You need to do your custom processing either with a custom backend or with a signal. I chose to create a custom back-end (if anyone has gotten this working with signals, please post code - I wasn't able to get it working that way). You should be able to modify this to save to your custom profile.

                  1. 在您的应用中创建一个 regbackend.py.
                  2. 将 DefaultBackend 中的 register() 方法复制到其中.
                  3. 在方法结束时,进行查询以获取对应的 User 实例.
                  4. 将其他表单字段保存到该实例中.
                  5. 修改 URL conf 使其同时指向自定义表单和自定义后端

                  所以 URL conf 是:

                  So the URL conf is:

                  url(r'^accounts/register/$',
                      register,
                      {'backend': 'accounts.regbackend.RegBackend','form_class':MM_RegistrationForm},        
                      name='registration_register'
                      ),
                  

                  regbackend.py 具有必要的导入,基本上是 DefaultBackend 的副本,只有 register() 方法,并添加了:

                  regbackend.py has the necessary imports and is basically a copy of DefaultBackend with just the register() method, and the addition of:

                      u = User.objects.get(username=new_user.username)
                      u.first_name = kwargs['first_name']
                      u.last_name = kwargs['last_name']
                      u.save() 
                  

                  这篇关于Django-注册&amp;Django-Profile,使用您自己的自定义表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                  • <bdo id='Zjm2N'></bdo><ul id='Zjm2N'></ul>
                      <legend id='Zjm2N'><style id='Zjm2N'><dir id='Zjm2N'><q id='Zjm2N'></q></dir></style></legend>
                      <i id='Zjm2N'><tr id='Zjm2N'><dt id='Zjm2N'><q id='Zjm2N'><span id='Zjm2N'><b id='Zjm2N'><form id='Zjm2N'><ins id='Zjm2N'></ins><ul id='Zjm2N'></ul><sub id='Zjm2N'></sub></form><legend id='Zjm2N'></legend><bdo id='Zjm2N'><pre id='Zjm2N'><center id='Zjm2N'></center></pre></bdo></b><th id='Zjm2N'></th></span></q></dt></tr></i><div id='Zjm2N'><tfoot id='Zjm2N'></tfoot><dl id='Zjm2N'><fieldset id='Zjm2N'></fieldset></dl></div>

                      <small id='Zjm2N'></small><noframes id='Zjm2N'>

                      <tfoot id='Zjm2N'></tfoot>

                            <tbody id='Zjm2N'></tbody>