1

好的,所以我有一个基于 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)

表格.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>
4

1 回答 1

1

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

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

于 2017-01-19T13:13:43.797 回答