Python 从0开始 一步步基于Django创建项目(10)用户登录和注销

发布于:2024-03-29 ⋅ 阅读:(20) ⋅ 点赞:(0)

Django项目中,已经包含了应用程序city_infos,用于存储城市相关信息,实现增删改查操作。

针对用户管理,需要新建项目user.

步骤如下:

1、创建新项目users

启动虚拟环境,导航至项目根目录:C:\D\Python\Python310\study\snap_gram,执行创建应用程序命令。

(sg_env) C:\D\Python\Python310\study\snap_gram>python manage.py startapp users

执行命令后,在根目录下新生成文件夹users,其结构与city_infos相同。

2、将应用程序users纳入Django项目

具体包含两步:

1)修改C:\D\Python\Python310\study\snap_gram\snap_gram目录下的settings.py文件

INSTALLED_APPS = [
    #my app
    'city_infos',
    'users',

2)修改同一路径下的urls.py文件

urlpatterns = [
    path('admin/', admin.site.urls),
    path('users/',include('users.urls')),
    path('',include('city_infos.urls')),
]

这行代码与任何以单词users打头的URL(如http://localhost:8000/users/login/)都匹配。

3、实现用户登录

1)新建C:\D\Python\Python310\study\snap_gram\users路径下的urls.py文件

定义users应用程序中的url,用于用户登录。

登录页面的URL模式与URL http://localhost:8000/users/login/匹配。

'''users urls'''

from django.urls import path, include

app_name = 'users'#命名空间

urlpatterns = [
    #通过include方法,使用Django定义的默认身份验证URL
    path('',include('django.contrib.auth.urls')),
]

2)新建C:\D\Python\Python310\study\snap_gram\users\templates\registration路径下的login.html文件

用户请求登录页面时,Django将使用一个默认的view函数,与之对应的html文件定义如下:

<!-- 一个应用程序中的模板可继承另一个应用程序中的模板 -->
{% extends "city_infos/base.html" %}

{% block content %}
    <!-- 设置表单的errors属性,表单是Django自带的 -->
    {% if form.errors %}
        <p>您的用户名或密码不匹配,请重试。</p>
    {% endif %}

    <!-- 对提交的登录信息进行处理 -->
    <form method="post" action="{% url 'users:login' %}">
        {% csrf_token %}
        {{ form.as_p }}<!-- 显示表单内容 -->
        <button name="submit">登录</button>
        <!-- next:登录后重定向 -->
        <input type="hidden" name="next"
               value="{% url 'city_infos:index' %}" />
    </form>
{% endblock content %}

3)修改C:\D\Python\Python310\study\snap_gram\city_infos\templates\city_infos路径下的base.html文件

增加‘登录’链接。

<p>
    <a href="{% url 'city_infos:index'%}">City Info</a>--
    <a href="{% url 'city_infos:cities'%}">Cities</a>--

    <!-- user是Django自带的,判断已经登录,则显示主页index.html信息
    否则,显示登录页面users/login.html信息 -->
    {% if user.is_authenticated %}
        Hello,{{user.username}}.
    {% else %}
        <a href="{% url 'users:login' %}">登录</a>
    {% endif %}
</p>

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

4、实现用户注销

1)修改C:\D\Python\Python310\study\snap_gram\users路径下的urls.py文件

定义用于注销的URL,127.0.0.1:8000/users/custom_logout。

urlpatterns = [
    #通过include方法,使用Django定义的默认身份验证URL
    path('',include('django.contrib.auth.urls')),
    #注销
    path('custom_logout/',views.custom_logout,name='custom_logout'),
]

2)新建C:\D\Python\Python310\study\snap_gram\users路径下的views.py文件

响应注销URL。

from django.shortcuts import render, redirect
from django.contrib.auth import logout

# Create your views here.
def custom_logout(request):
    if request.method == 'POST':
        logout(request)
        return redirect('city_infos:index')
    return render(request, 'users/logged_out.html')

3)新建C:\D\Python\Python310\study\snap_gram\users\templates\users路径下的logged_out.html文件。

在这一步中,html文件是我们自定义的注销页面,所以不能放在C:\D\Python\Python310\study\snap_gram\users\templates\registration路径下。

在服务器中,要求注销操作以post的方式提交。

{% extends "city_infos/base.html" %}

{% block content %}
  <h1>确认:</h1>
  <p>确认注销?</p>
  <form method="post">
    {% csrf_token %}
    <button name="submint">注销</button>
  </form>
  <a href="{% url 'city_infos:index' %}">取消</a>
{% endblock content %}

4)修改

添加注销操作链接。这个操作是<a打头,request提交的方式是GET,所以服务器不会执行注销操作,必须把GET提交,改变为POST提交

{% if user.is_authenticated %}
    Hello,{{user.username}}.
    <a href="{% url 'users:custom_logout' %}">注销</a>

以GET方式提交注销时,服务器提示:

Method Not Allowed (GET): /users/logout/

本文含有隐藏内容,请 开通VIP 后查看