创建用户账户

这一部分我们来创建用户注册和身份验证系统。

应用程序users

python manage.py startapp users

《python编程:从入门到实践学习笔记-Django用户账户(二)》
《python编程:从入门到实践学习笔记-Django用户账户(二)》

1.将应用程序users添加到settings.py

#--snip--

INSTALLED_APPS = (
    #--snip--
    'learning_logs',
    'users',
)
#--snip--

2.创建users的URL模式

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^users/', include('users.urls', namespace='users')),
    url(r'', include('learning_logs.urls', namespace='learning_logs')),
]
http://localhost:8000/users/login/

登录页面

在learning_log/users/中新建一个urls.py。

from django.conf.urls import url
from django.contrib.auth.views import loginfrom . import views

urlpatterns = [
    url(r'^login/$', login, {'template_name': 'users/login.html'},name='login'),
]
http://localhost:8000/ users/login/users/templates/users/
{% extends "learning_logs/base.html" %}

{% block content %}

  {% if form.errors %}
  <p>Your username and password didn't match. Please try again.</p>
  {% endif %}
  
  <form method="post" action="{% url 'users:login' %}">
  {% csrf_token %}
  {{ form.as_p }}
  
  <button name="submit">log in</button>
  <input type="hidden" name="next" value="{% url 'learning_logs:index' %}" />
  </form>

{% endblock content %}
{% if %}
<p>
  <a href="{% url 'learning_logs:index' %}">Learning Log</a> - 
  <a href="{% url 'learning_logs:topics' %}">Topics</a> - 
  {% if user.is_authenticated %}
    Hello, {{ user.username }}.
  {% else %}
    <a href="{% url 'users:login' %}">log in</a>
  {% endif %}
</p>

{% block content %}{% endblock content %}
is_authenricatedusernamehttp://127.0.0.1:8000/adminhttp://127.0.0.1:8000/users/login/

《python编程:从入门到实践学习笔记-Django用户账户(二)》
《python编程:从入门到实践学习笔记-Django用户账户(二)》

《python编程:从入门到实践学习笔记-Django用户账户(二)》
《python编程:从入门到实践学习笔记-Django用户账户(二)》

注销

1.注销URL

#users/urls.py
from django.conf.urls import url
from django.contrib.auth.views import login
from . import views

urlpatterns = [
    url(r'^login/$', login, {'template_name': 'users/login.html'},name='login'),
    url(r'^logout/$', views.logout_view, name='logout'),
]
logout()
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth import logout

def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse('learning_logs:index'))
logout(){% if user.is_authenticated %}
#base.html
<p>
  <a href="{% url 'learning_logs:index' %}">Learning Log</a> - 
  <a href="{% url 'learning_logs:topics' %}">Topics</a> - 
  {% if user.is_authenticated %}
    Hello, {{ user.username }}.    
    <a href="{% url 'users:logout' %}">log out</a>
  {% else %} 
    <a href="{% url 'users:login' %}">log in</a>
  {% endif %}
</p>

{% block content %}{% endblock content %}

运行服务器可以看到

《python编程:从入门到实践学习笔记-Django用户账户(二)》
《python编程:从入门到实践学习笔记-Django用户账户(二)》

注册页面

1.注册页面的URL模式

from django.conf.urls import url
from django.contrib.auth.views import login
from . import views

urlpatterns = [
    url(r'^login/$', login, {'template_name': 'users/login.html'},name='login'),
    url(r'^logout/$', views.logout_view, name='logout'),
    url(r'^register/$', views.register, name='register'),
]
register()
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.forms import UserCreationForm

def logout_view(request):
    --snip--

def register(request):
    if request.method != 'POST':
        #显示空的注册表单
        form = UserCreationForm();
    else:        
        #处理填写好的表单
        form = UserCreationForm(data=request.POST) 
               
        if form.is_valid():
            new_user = form.save()            
            #让用户自动登录并重定向到主页
            authenticate_user = authenticate(username=new_user.username,
                password=request.POST['password1'])
            login(request, authenticate_user)            
            return HttpResponseRedirect(reverse('learning_logs:index'))

    context = {'form': form}    
    return render(request, 'users/register.html', context)
login()authenticate()save()authenticate()'password1'authenticate()login()
{% extends "learning_logs/base.html" %}

{% block content %}

  <form method="post" action="{% url 'users:register' %}">
    {% csrf_token %}
    {{ form.as_p }}
  
    <button name="submit">register</button>
    <input type="hidden" name="next" value="{% url 'learning_logs:index' %}" />
  </form>

{% endblock content %}

4.链接到注册页面

<p>
  <a href="{% url 'learning_logs:index' %}">Learning Log</a> - 
  <a href="{% url 'learning_logs:topics' %}">Topics</a> - 
  {% if user.is_authenticated %}
    Hello, {{ user.username }}.
    <a href="{% url 'users:logout' %}">log out</a>
  {% else %}
    <a href="{% url 'users:register' %}">register</a> - 
    <a href="{% url 'users:login' %}">log in</a>
  {% endif %}
</p>

{% block content %}{% endblock content %}

运行服务器可以看到

《python编程:从入门到实践学习笔记-Django用户账户(二)》
《python编程:从入门到实践学习笔记-Django用户账户(二)》

《python编程:从入门到实践学习笔记-Django用户账户(二)》
《python编程:从入门到实践学习笔记-Django用户账户(二)》