django登录与注销学习笔记-创新互联

创建工程

django-admin startproject 工程名字

创新互联建站专注于企业成都全网营销推广、网站重做改版、桥东网站定制设计、自适应品牌网站建设、HTML5建站商城网站建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为桥东等各大城市提供网站开发制作服务。

创建项目

python manage.py startapp 应用名
django登录与注销学习笔记

注册django后台管理代码

#后台认证数据表迁移
D:\pythonspacen\djano\guest>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK

#创建用户
D:\pythonspacen\djano\guest>manage.py createsuperuser
Username (leave blank to use 'admin'):
Email address: admin@admin.com
Password:
Password (again):
The password is too similar to the email address.
Password:
Password (again):
Superuser created successfully.

编写配置路由代码

from django.contrib import admin
from django.urls import path

from sign import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index),
    path('index/', views.index),
    path('accounts/login/', views.index),  # 认证系统
    path('Login/', views.Login),
    path('logout/', views.logout),  # 退出系统
    path('event_manage/', views.event_manage),
]

编写视图代码

from django.contrib.messages.storage import session
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth.decorators import login_required

def index(request):
    return render(request, "index.html")

# 使用django登录
def Login(request):
    if request.method == 'POST':
        login_username = request.POST.get('username', '')
        login_password = request.POST.get('password', '')
        # 使用框架系统的用户系统
        user = auth.authenticate(username=login_username, password=login_password)
        if user is not None:
            auth.login(request, user)
            request.session['user'] = login_username
            response = HttpResponseRedirect('/event_manage/')
            return response
        elif login_password == "" and login_username == "":
            return render(request, "index.html", {'error': 'username or password  null'})
        else:
            return render(request, "index.html", {'error': 'username or password error!'})
    else:
        return render(request, "index.html")

# 列表
@login_required
def event_manage(request):
    # username = request.COOKIES.get("user", "")
    username = request.session.get('user', '')
    conten = {'infon': "login success!", 'user': username}
    return render(request, "event_manage.html", conten)

# 退出
@login_required
def logout(request):
    # 使用框架系统退出
    auth.logout(request)
    # 调转到页面
    response = HttpResponseRedirect('/index/')
    return response

启动项目

django登录与注销学习笔记

使用刚才注册的用户名与密码登录
django登录与注销学习笔记
django登录与注销学习笔记

登录成功

django登录与注销学习笔记

inde.html




    
    登录页面
    
    
    
    
    



{{ error }}

{% csrf_token %}

页面跳转




    
    发布会列表


退出
 Welcome,{{ user }}

{{ infon }}

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:django登录与注销学习笔记-创新互联
文章网址:http://ybzwz.com/article/ccsgpg.html