Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : Invalid password format or unknown hashing algorithm

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.

I got this type of my password in admin:

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.Example of my problem

What should I do to solve the problem?

Thank you!

like image 999
prog.Dusan Avatar asked Mar 12 '13 14:03

prog.Dusan


1 Answers

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.

like image 96
Andrew Ingram Avatar answered Oct 09 '22 09:10

Andrew Ingram