【问题标题】:Django UserCreationForm for custom model is outputting 3 password fields password, password1, and password2自定义模型的 Django UserCreationForm 正在输出 3 个密码字段密码、密码 1 和密码 2
【发布时间】:2013-11-18 18:07:30
【问题描述】:

好的,所以我有一个基于 AbstractUser 的用户注册表单,它替换了我的 settings.py 中的 Django 模型,当我测试用户的注册表单时,该模型只添加了我需要的用户信息的几个字段没有被创建,我调试并发现最后一次登录和日期都在需要的地方加入,所以我将它们设置为隐藏输入,值为

{% now "SHORT_DATE_FORMAT" %} 

解决了前两个错误,但是还有第三个错误说字段密码是必需的,即我的表单要求我输入密码、密码 1 和密码 2

非常感谢您的帮助!

这是我的观点.py

def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)

        if form.is_valid():
            new_user = form.save(commit=False)
            new_user.is_active = True
            new_user.save()
            group = Group.objects.get(name='Usuario')
            new_user.groups.add(group)
            return HttpResponseRedirect('/')

   else:
       form = UserRegisterForm()

   return render_to_response('register.html', locals(), RequestContext(request))

这是我的模特

class SiteUser(AbstractUser):
    descripcion = models.TextField(blank=True, null=True)
    direccion = models.CharField(max_length=255, blank=True, null=True)
    image = models.ImageField(upload_to='imagenes_usuarios', blank=True, null=True)
    thumbnail = models.ImageField(upload_to='imagenes_usuarios/thumbs', blank=True, null=True)
    slug = models.SlugField(default='', blank=True)

Forms.py

class UserRegisterForm(UserCreationForm):
    class Meta:
        model = SiteUser

    def clean_username(self):
        username = self.cleaned_data["username"]
        try:
            # Not sure why UserCreationForm doesn't do this in the first place,
            # or at least test to see if _meta.model is there and if not use User...
            self._meta.model._default_manager.get(username=username)
        except self._meta.model.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])

这是我的模板

<div class="content">
    <form class="registration" method="POST" action="{% url 'register' %}">
        {% csrf_token %}
        <input type="hidden" name="date_joined" value="{% now "SHORT_DATETIME_FORMAT" %}">
        <input type="hidden" name="last_login" value="{% now "SHORT_DATETIME_FORMAT" %}">

        <div class="row">
            <div class="form-group {% if form.username.errors %}has-error{% endif %} col-xs-6">
                <label for="{{ form.username.id_for_label }}"
                       class="control-label">{{ form.username.label }}</label>
                <input type="text" id="{{ form.username.id_for_label }}" class="form-control"
                       name="{{ form.username.html_name }}">
                {% for error in form.username.errors %}
                    <span class="help-block">{{ error }}</span>
                {% endfor %}
            </div>
            <div class="form-group {% if form.first_name.errors %}has-error{% endif %} col-xs-6">
                <label for="{{ form.first_name.id_for_label }}"
                       class="control-label">{{ form.first_name.label }}</label>
                <input type="text" id="{{ form.first_name.id_for_label }}" class="form-control"
                       name="{{ form.first_name.html_name }}">
                {% for error in form.first_name.errors %}
                    <span class="help-block">{{ error }}</span>
                {% endfor %}
            </div>
        </div>
        <div class="row">
            <div class="form-group {% if form.last_name.errors %}has-error{% endif %} col-xs-6">
                <label for="{{ form.last_name.id_for_label }}"
                       class="control-label">{{ form.last_name.label }}</label>
                <input type="text" id="{{ form.last_name.id_for_label }}" class="form-control"
                       name="{{ form.last_name.html_name }}">
                {% for error in form.last_name.errors %}
                    <span class="help-block">{{ error }}</span>
                {% endfor %}
            </div>
            <div class="form-group {% if form.email.errors %}has-error{% endif %} col-xs-6">
                <label for="{{ form.email.id_for_label }}"
                       class="control-label">{{ form.email.label }}</label>
                <input type="text" id="{{ form.email.id_for_label }}" class="form-control"
                       name="{{ form.email.html_name }}">
                {% for error in form.email.errors %}
                    <span class="help-block">{{ error }}</span>
                {% endfor %}
            </div>
        </div>
        <div class="row">
            <div class="form-group {% if form.password1.errors %}has-error{% endif %} col-xs-6">
                <label for="{{ form.password1.id_for_label }}"
                       class="control-label">{{ form.password1.label }}</label>
                <input type="password" id="{{ form.password1.id_for_label }}" class="form-control"
                       name="{{ form.password1.html_name }}">
                {% for error in form.password1.errors %}
                    <span class="help-block">{{ error }}</span>
                {% endfor %}
            </div>
            <div class="form-group {% if form.password2.errors %}has-error{% endif %} col-xs-6">
                <label for="{{ form.password2.id_for_label }}"
                       class="control-label">{{ form.password2.label }}</label>
                <input type="password" id="{{ form.password2.id_for_label }}" class="form-control"
                       name="{{ form.password2.html_name }}">
                {% for error in form.password2.errors %}
                    <span class="help-block">{{ error }}</span>
                {% endfor %}
            </div>
        </div>
        <input type="submit">

    </form>
</div>

【问题讨论】:

  • 我不知道你的问题的答案,但如果你还有机会转换技术,我建议你看看django-registrationcrispy-forms 来做这种事情容易得多。
  • @Garry 谢谢你的建议!!我考虑过这一点,但是我不确定 django-registration 是否需要太多 tweeking 才能使用自定义用户模型,是这样吗,我似乎无法找到有关此的官方文档

标签: django django-models django-forms django-views django-users


【解决方案1】:

由于您使用的是 UserCreationForm,它需要所有字段来验证和保存表单,为什么不试试 ModelForm

请参考:https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/

【讨论】:

猜你喜欢
  • 2021-04-27
  • 1970-01-01
  • 2020-10-26
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
  • 2011-04-12
  • 2019-08-01
  • 2012-12-27
相关资源
最近更新 更多