【问题标题】:How to add extra (custom) fields in Django-registration 1.0如何在 Django-registration 1.0 中添加额外(自定义)字段
【发布时间】:2014-12-25 06:37:19
【问题描述】:

我已经阅读了很多关于此的问题和答案,但仍然没有好运。例如here 是一个很好的答案,但很可能不适用于 django-registration 1.0。

我的目标是在注册表单中添加两个自定义字段,即 organizationposition注意:我使用的是registration.backend.simple提供的一步式django注册。

【问题讨论】:

  • 为什么说答案不适用于django-registration 1.0?
  • @ChrisHawkes 我收到“无法导入名称注册”错误。我认为注册在 django-registration 1.0 中被剥夺了。我尝试使用“url(r'^register/$', RegistrationView.as_view(form_class=UserRegForm))”。但仍然没有运气
  • 根据为使用新版本的 django-registration 修复该错误而提供的答案之一,您必须从 django.conf.urls 导入模式、包含、来自 registration.backends 的 url 添加此内容。 default.views import RegistrationView from abby.apps.accounts.forms import UserRegForm urlpatterns = patterns('', url(r'^register/$', RegistrationView.as_view(form_class=UserRegForm)), ) stackoverflow.com/questions/14726725/…
  • @ChrisHawkes 很抱歉,这也不起作用。我在之前的评论中已经提到我已经尝试过了。
  • @Iqbal 如果您仍有问题,请在尝试 Chris 建议后编辑您的描述以包含异常或问题。或者,如果您解决了问题,请发布解决方案以帮助其他有类似问题的读者

标签: django django-registration


【解决方案1】:

由于您还没有答案,我提供了这个答案,尽管这并不是您所要求的。不过,我认为它可能对您有所帮助...

这就是我在一些使用 django-registration 1.0、Python 2.7 和 Django 1.6 的项目中所做的。在这种情况下,我只需使用usernameemailpassword 进行注册,然后用户可以在注册后添加个人资料字段。修改它以在注册时接受这些字段应该不会太难。

我使用 Twitter Bootstrap 进行样式设置,所以我的模板可能对你有帮助,也可能对你没有帮助。在这种情况下,我将它们排除在外。

我创建了几个名为 accountsauthentication 的应用程序,用于保存我的模型、表单和视图:

accounts.models.UserProfile

from django.db import models

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    title = models.CharField(max_length=100)
    company = models.CharField(max_length=250)

authentiction.forms.EditUserProfileForm

from django.forms import ModelForm

class EditUserProfileForm(ModelForm):**
    . . .
    title = forms.CharField(widget=forms.TextInput())
    company = forms.CharField(widget=forms.TextInput())
    . . .

accounts.views.EditUserProfileView

from django.views.generic.base import View
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect

from .models import UserProfile
from .forms import EditUserProfileForm

class EditUserProfileView(View):

    form_class = EditUserProfileForm
    template_name = 'accounts/profile_edit.html'

    @method_decorator(login_required)
    def get(self, request, *args, **kwargs):

        profile = get_object_or_404(UserProfile, user=request.user)

        form_dict = {
            'title': profile.title,
            'company': profile.company,
        }

        form = self.form_class(form_dict)

        return render(request, self.template_name, {
            'form': form,
            'profile': profile,
        })

    @method_decorator(login_required)
    def post(self, request, *args, **kwargs):

        profile = get_object_or_404(UserProfile, user=request.user)

        form = self.form_class(request.POST, instance=profile)

        if form.is_valid():

            company = form.cleaned_data['company']
            title = form.cleaned_data['title']

            title.company = title
            profile.company = company

            # you might need to save user too, depending on what fields
            request.user.save()
            profile.save()

            return HttpResponseRedirect('/dashboard/')

        return render(request, self.template_name, {'form': form})

project.urls

url(r'^accounts/profile/edit/', EditUserProfileView.as_view(),  name='edit_user_profile_view'),

【讨论】:

    猜你喜欢
    • 2015-06-19
    • 1970-01-01
    • 2013-01-21
    • 2013-06-27
    • 2011-04-14
    • 1970-01-01
    • 2011-02-25
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多