2017-06-15 144 views
0

我已经创建了扩展用户模型的员工模型。当我创建一名员工时,我也可以在User.objects中找到相同的用户,以便我知道它已创建。当我尝试登录与用户的凭据的凭证没有通过认证(不正确的用户名或密码)Django登录与模型扩展用户

class Employee(User): 
start_date = models.DateField() 

@property 
def leave_days_remaining(self): 
    #to calculate 
    calculated_days=10 
    return calculated_days 



def trial(request): 

emp = Employee.objects.create(username='lll', password='pass', 
email="[email protected]", first_name='Mokgadi,   
    last_name='Rasekgala', start_date=datetime.date.today()) 
found=User.objects.get(username='lll') 
print found.email 
print found.username 
print found.password #Found exists 
return render(request, 'leave/trial.html') 






{% if form.errors %} 
     <p>{{ form.errors }}Your username and password didn't match. Please try again.</p> 
{% endif %} 
<form method="post" action="{% url 'login' %}"> 
    {% csrf_token %} 
    <p> 
     <label>Username</label> 
     <input type="text" name="username"> 
    </p> 
    <p> 
     <label>Password</label> 
     <input type="password" name="password"> 
    </p> 
    <button type="submit">Login</button> 
</form> 
+1

您需要使用'create_user',而不是'创建'。或者你需要散列密码。 – Brobin

回答

2

对于创建一个可以登录,密码必须加密的用户。您可以通过两种不同的方式来达到此目的。

首先,你可以使用create_user代替create

emp = Employee.objects.create_user(...)

或者你也可以设置密码散列:

emp = Employee.objects.create(...) emp.set_password("password")

+0

非常感谢。 –