问题
Ok, so I have a user registration form based on AbstractUser that replaces the Django model in my settings.py, the model only adds a couple of fields I needed for info on the users, when I tested the registration form the user did not get created, I debugged and found that both last login and date joined where required so I set them as hidden inputs with value of
{% now "SHORT_DATE_FORMAT" %}
that solved the first two errors however there is a third error that says the field password is required, that is my form is asking me for, password, password1, and password2
I would really appreciatte your help!
This is my views.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))
This is my model
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'])
And here is my template
<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>
回答1:
Since you are using UserCreationForm it requires all the fields to validate and save the form, why not try ModelForm instead.
Refer this: https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/
来源:https://stackoverflow.com/questions/19830126/django-usercreationform-for-custom-model-is-outputting-3-password-fields-passwor