I have problem when I want to use simple CreateView for creating new user. Everything is ok, I can create user, but when I want logging with new created user, I cant do it.
Invalid password format or unknown hashing algorithm.
Raw passwords are not stored, so there is no way to see this user's password, but you can change the password using this form.
This is my code:
model.py
from django.db import models
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import AbstractUser, UserManager
class CustomUser(AbstractUser):
"""app setting of user"""
url = models.URLField(u'Site URL', max_length=100, null=True, unique=True)
admin.site.register(CustomUser, UserAdmin)
urls.py
from django.conf.urls.defaults import *
from models import CustomUser
from django.views.generic import CreateView
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
urlpatterns = patterns('',
url(r'^create_user/$',(CreateView.as_view(model=CustomUser, get_success_url =lambda: reverse('create_user'),
template_name="create_user.html")), name='create_user'),
)
create_user.html
<div title="Create Form " id="/create_user">
<p> <b>Create account:</b> </p>
{{ form.errors }}
<form action="" method="post" id="id_create_user" >
{% csrf_token %}
{{ form.as_p }}
<div>
<input type="submit" value="Create account" style="">
</div>
</form>
</div>
And after creating user, I have to log in as admin and change this password, and after that everything will be ok.
What should I do to solve the problem?
Thank you!
The problem is that CreateView is generating a form automatically based on the model, which means it's writing directly to the password field rather than using the correct API for creating a user.
Try this form instead, it'll handle it for you:
from django.contrib.auth.forms import UserCreationForm
urlpatterns = patterns('',
url(r'^create_user/$',(CreateView.as_view(model=CustomUser, get_success_url =lambda: reverse('create_user'), form_class=UserCreationForm, template_name="create_user.html")), name='create_user'),
)
These forms essentially give you the same as what you see in the admin, which is a good starting point, if you're using a custom user model you might want to do some additional work to get your customisations in.
Here's the source code for the supplied UserCreationForm: https://github.com/django/django/blob/stable/1.5.x/django/contrib/auth/forms.py#L61-L110
You can re-implement it yourself as a normal ModelForm, the key thing you must include is the save method where user.set_password
is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With