django-admin startproject 工程名字
創(chuàng)新互聯(lián)建站專注于企業(yè)成都全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、橋東網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為橋東等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。python manage.py startapp 應(yīng)用名
#后臺(tái)認(rèn)證數(shù)據(jù)表遷移
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
#創(chuàng)建用戶
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), # 認(rèn)證系統(tǒng)
path('Login/', views.Login),
path('logout/', views.logout), # 退出系統(tǒng)
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', '')
# 使用框架系統(tǒng)的用戶系統(tǒng)
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):
# 使用框架系統(tǒng)退出
auth.logout(request)
# 調(diào)轉(zhuǎn)到頁(yè)面
response = HttpResponseRedirect('/index/')
return response
使用剛才注冊(cè)的用戶名與密碼登錄
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>登錄頁(yè)面</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet">
<style type="text/css">
#red {
color: red;
}
</style>
</head>
<body>
<div class="container">
<form class="form-horizontal" action="/Login/" method="post">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">用戶名: </label>
<div class="col-sm-10">
<input type="text" name="username" class="form-control-static" id="inputEmail3" placeholder="username">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密碼:</label>
<div class="col-sm-10">
<input type="password" name="password" class="form-control-static" id="inputPassword3"
placeholder="Password">
</div>
</div>
<p id="red" >{{ error }}</p>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"> 記住
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">登錄</button>
</div>
</div>
{% csrf_token %}
</form>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依賴 jQuery,所以必須放在前邊) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加載 Bootstrap 的所有 JavaScript 插件。你也可以根據(jù)需要只加載單個(gè)插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>發(fā)布會(huì)列表</title>
</head>
<body>
<a href="/logout/">退出</a>
<a href="/event_manage/"> Welcome,{{ user }}</a>
<h3>{{ infon }}</h3>
</body>
</html>
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
本文名稱:django登錄與注銷學(xué)習(xí)筆記-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://jinyejixie.com/article36/ccsgpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、微信公眾號(hào)、動(dòng)態(tài)網(wǎng)站、軟件開(kāi)發(fā)、App開(kāi)發(fā)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容