// models.py
from django.contrib.auth.models import AbstractUser
from django.contrib.sessions.models import Session
class CustomUser(AbstractUser):
addr1= models.CharField(max_length=20)
addr2= models.CharField(max_length=20)
city= models.CharField(max_length=20)
state= models.CharField(max_length=20)
forms.py
from django.contrib.auth.forms import AuthenticationForm
from django import forms
class LoginForm(AuthenticationForm):
username = forms.CharField(label="Username", max_length=30,
widget=forms.TextInput(attrs={'class': 'form-control', 'name': 'username'}))
password = forms.CharField(label="Password", max_length=30,
widget=forms.TextInput(attrs={'class': 'form-control', 'name': 'password'}))
// project / urls.py(外部的)
from django.contrib.auth import views
from student.forms import LoginForm
url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}, name='login'),
url(r'^logout/$', views.logout, {'next_page': '/home'}),
// login.html(登录模板)
<div class="container">
<section id="content">
<form action="{% url 'login' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<h1>Login Form</h1>
<div class="imgcontainer">
<img src="{% static 'student/patient.jpg' %}" alt="Avatar" class="avatar">
</div>
<div class="username">
{{ form.username.label_tag }}
{{ form.username }}
</div>
<div class="password">
{{ form.password.label_tag }}
{{ form.password }}
</div>
<div class="submitb">
<input type="submit" value="Log In" name="mybtn">
</div>
<div class="resetb">
<input type="submit" value="Reset">
<a href="#forgotpwd">Forgot password?</a>
</div>
<input type="hidden" name="next" value="{{ next }}" />
</form>
</section>
</div>
这是settings.py //settings.py
LOGIN_REDIRECT_URL = '/login/sample'
是以这种方式登录和注销时在这里调用的login()和logout()吗?...如果没有那么我可以扩展django.contrib.auth的login()和logout()吗?
答案 0 :(得分:1)
是的,你也可以扩展登录
settings.py
LOGIN_REDIRECT_URL = 'the name of the url'
LOGOUT_URL = 'the name of the url'
urls.py
from django.contrib.auth.views import login, logout
from django.core.urlresolvers import reverse_lazy
url(r'^login/$', login, {'template_name': 'login.html'}, name='login'),
url(r'^logout/', logout, {'next_page': reverse_lazy('the name of the url')}, name='logout'),
的login.html
<form method="post" action="{% url 'login' %}">
<div class="form-group">
{% csrf_token %}
<label for="username">Usuario</label>
<input id="username" type="text" name="username" class="form-control" required placeholder="Usuario">
</div>
<div class="form-group">
<label for="password">Contraseña</label>
<input id="password" type="password" name="password" class="form-control" required placeholder="Contraseña">
</div>
<button class="btn btn-primary btn-block" type="submit">Ingresar</button>