BBS項目--登錄

BBS階段性測試總要求

django登錄報錯

Error: [WinError 10013] 以一種訪問權限不允許的方式做了一個訪問套接字的嘗試。

原因分析:出現這種情況在Windows中很常見,就是端口被占用

解決措施:這時我們只需改一下端口便可以了

登錄前端頁面(HTML)

# 這次頁面采用的是bookstrip5:Bootstrap 入門 · Bootstrap v5 中文文檔 v5.3 | Bootstrap 中文網 (bootcss.com)

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>

<style>html,body {height: 100%;}body {display: flex;align-items: center;padding-top: 40px;padding-bottom: 40px;background-color: #f5f5f5;}.form-signin {max-width: 330px;padding: 15px;}.form-, .form-floating:focus-within {z-index: 2;}.form-signin input[type="email"] {margin-bottom: -1px;border-bottom-right-radius: 0;border-bottom-left-radius: 0;}.form-signin input[type="password"] {margin-bottom: 10px;border-top-left-radius: 0;border-top-right-radius: 0;a {color: rgba(1, 4, 12, 0.92);ext-decoration: none;}}
</style>
# body部分:
<body>
<main class="form-signin w-100 m-auto"><div class="text-center"><div class="form-group"><label for="id_avatar"><img class="mb-4 "src="https://tse3-mm.cn.bing.net/th/id/OIP-C.VeJFhbdc95msPtA2RFHHbwAAAA?rs=1&pid=ImgDetMain"alt="" height="80px" width="80px" id="id_img1"style="margin-left: 20px"></label><input type="file" id="id_avatar" class="form-control" accept="image/*" style="display: none"><h1 class="h3 mb-3 fw-normal">糖果愛上我</h1></label></div></div><form id="login_form">{% csrf_token %}<div class="form-floating"><div class="group-group"><label for="id_username">用戶名</label><input type="text" name="username" class="form-control"id="id_username"><span class="pull-right error" style="color: red"> </span></div><div class="form-group"><label for="floatingInput">密碼</label><input type="password" name="password" class="form-control"id="id_password"><span class="pull-right error" style="color: red"> </span></div><div class="form-group"><label for="floatingInput">驗證碼</label><div class="row"><div class="col-md-6"><input type="text" name="code" class="form-control" id="id_code"></div><img src="/get_valid_code/" alt="" class="col-md-6" height="35" id="id_img"></div></div></div></form><div class="w-100 btn btn-lg btn-primary" id="id_submit" style="margin-top: 20px">登錄</div><span class="error" style="color: darkred;margin-left: 10px" id="id_error"></span><p style="color: #888888">您還沒賬號嗎?那我們先注冊一個吧~ <a href="/register/">滴滴</a></p><p class="mt-5 mb-3 text-muted">* 佳祺今天也要加油鴨</p>
</main>
</body>

驗證碼功能

# 能夠顯示驗證碼圖片,隨機改變驗證碼,點擊圖片就會自己刷新驗證碼

# 視圖層后端,自定義驗證碼,驗證碼用session進行保存,方便后面驗證是否正確:

from django.shortcuts import render, HttpResponse, redirect
from PIL import Image, ImageDraw, ImageFont
from .utills import get_random_code, get_random_rgb
from django.contrib.auth import authenticate, login as auth_login, logout as auth_logout
import random, json
from io import BytesIOdef get_valid_code(request):height = 38width = 300image_tmp = Image.new('RGB', (300, 38), (255, 255, 255))# 把空圖片放在了畫板上,就可以寫字了draw = ImageDraw.Draw(image_tmp)# 加入字體img_font = ImageFont.truetype('./static/font/ss.TTF', 23)# 驗證碼code_str = get_random_code()print(code_str)# 重要,要保存request.session['code'] = code_strfor i, item in enumerate(code_str):draw.text((30 + i * 50, 3), item, fill=get_random_rgb(), font=img_font)  # (x軸,y軸),字,字顏色,字體# 增加難度--->在圖片上畫點for i in range(30):draw.point([random.randint(0, width), random.randint(0, height)], fill=get_random_rgb())# 畫弧形x = random.randint(0, width)y = random.randint(0, height)draw.arc((x, y, x + 4, y + 4), 0, 90, fill=get_random_rgb())# 在圖片上劃線for i in range(3):x1 = random.randint(0, width)x2 = random.randint(0, height)y1 = random.randint(0, width)y2 = random.randint(0, height)draw.line((x1, y1, x2, y2), fill=get_random_rgb())# 放在內存中,一旦不用,自動清理內存my_io = BytesIO()image_tmp.save(my_io, 'png')return HttpResponse(my_io.getvalue())

# 講圖片渲染在前端html頁面:

<div class="form-group"><label for="floatingInput">驗證碼</label><div class="row"><div class="col-md-6"><input type="text" name="code" class="form-control" id="id_code"></div><img src="/get_valid_code/" alt="" class="col-md-6" height="35" id="id_img"></div>
</div>

# 設置驗證碼id,設置點擊事件,使點擊驗證碼圖片會自己刷新驗證碼:

<script>$('#id_img').click(function () {// img標簽有個特性:只要src變了,就會重新src地址請求拿數據var url = $(this).attr('src') + '?'  // /get_valid_code/???console.log(url)$(this).attr('src', url)})
</script>

圖片渲染在前端配置

# 在django中,有兩個額外自己建立的文件包:

media:一般存入其他用戶上傳的圖片等等

static: 一般用于自身寫入的引入樣式,圖片等等? ? ? ?

# 因此,存入的圖片,我們想要渲染在前端頁面,也需配置成靜態文件

# settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'# 路由
path('media/<path:path>', serve, {'document_root': settings.MEDIA_ROOT}),

ajax提交登錄

# 前端把用戶提交的數據發送到后端,設置button點擊事件,用ajax提交數據:

<script>
$('#id_submit').click(function () {var serialize_data = $('#login_form').serializeArray()console.log(serialize_data)var data = {}var username = $('[name="username"]').val()var password = $('[name="password"]').val()var code = $('[name="code"]').val()var csrfmiddlewaretoken = $('[name="csrfmiddlewaretoken"]').val()data['username'] = usernamedata['password'] = passworddata['code'] = codedata['csrfmiddlewaretoken'] = csrfmiddlewaretokenconsole.log(data)$.ajax({url: '',method: 'post',data: data,success: function (data) {console.log(data)if (data.code == 100) {location.href = data.url} else {$("#id_error").html(data.msg)}}})})
</script>

# 后端拿到前端數據,先對驗證碼進行對比,然后使用auth模塊直接講用戶名和密碼與數據庫中的進行比較,返回給前端信息,成功便直接跳轉到主頁面:

def login(request):if request.method == 'GET':return render(request, 'login.html')else:username = request.POST.get('username')password = request.POST.get('password')net_code = request.POST.get('code').lower()#code = request.POST.get('code').lower()  # 會存在bug# 1 校驗驗證碼,取出老驗證碼,忽略大小寫old_code = request.session.get('code').lower()if code == old_code:# 2 去驗證用戶了---》user = authenticate(username=username, password=password)if user:# 登錄成功--->內部寫session了auth_login(request, user)return JsonResponse({'code': 100, 'msg': '登錄成功', 'url': '/'})else:return JsonResponse({'code': 101, 'msg': '用戶名或密碼錯誤'})else:return JsonResponse({'code': 102, 'msg': '驗證碼錯誤'})

今日思維導圖:

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274594.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274594.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274594.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【聲明】

我的公眾號和朋友圈有時會有一些課程推廣廣告&#xff0c;微博的收入來源。我接的廣告一般來說都是比自己去買會優惠不少&#xff0c;我也會想方設法爭取到更多福利&#xff08;優惠&#xff09;。買過的都知道確實優惠。如果有人看到覺得不合適&#xff0c;不想看到&#xff0…

Win7 訪問共享時輸入正確密碼仍然提示密碼錯誤

1、直接按下winr鍵&#xff0c;輸入secpol.msc&#xff0c;打開本地安全策略。 2、找到“安全設置”的“本地策略”的“安全選項” 3、在右邊一欄找到“網絡安全&#xff1a;LAN管理器身份驗證級別”&#xff0c;雙擊進入 4、在默認狀態選項下&#xff0c;英文版應該為"no…

怎么實現頁面友好跳轉_如何實現軟,友好和一致的UI設計

怎么實現頁面友好跳轉重點 (Top highlight)Design trends are constantly changing, aren’t they? Each month there is a new visual effect or a trick that becomes “設計趨勢在不斷變化&#xff0c;不是嗎&#xff1f; 每個月都有一個新的視覺效果或技巧&#xff0c;成為…

前端趨勢 2022

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信lxchuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外&…

MySQL Connector/ODBC 5.2.2 發布

MySQL Connector/ODBC 5.2.2 發布&#xff0c;這是一個穩定版本&#xff0c;下載地址&#xff1a; http://dev.mysql.com/downloads/connector/odbc/5.2.html MySQL Connector/ODBC 是 MySQL 官方發布的 ODBC 驅動程序包。轉載于:https://www.cnblogs.com/shihao/archive/2012/…

優秀測試管理工具必備九大功能分析

摘要&#xff1a;測試管理工具對測試的重要性毋庸質疑&#xff0c;兩位筆者有著多年的測試實戰經驗&#xff0c;對市面上的一些測試管理工具有過一定的研究&#xff0c;還根據目前比較流行的敏捷開發過程設計了一款測試管理工具。 這篇文章算是對這個設計過程的總結與分享&…

lightroom預設使用_在Lightroom中使用全景圖增強照片游戲

lightroom預設使用Everyone here has taken a panorama with an iphone. We’ve spun around in a circle, trying to keep that arrow right on the line, and more than likely ended up with a strange, squiggly, horizontal photo. Every so often you might get lucky an…

第91次TC39會議舉行,這還是我認識的JS嗎?

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

android調節音量——AudioManager的應用

Android中可以通過程序獲取系統手機的鈴聲和音量。同樣&#xff0c;也可以設置鈴聲和音量。Android中給出了AudioManager類來實現音量獲取、音量控制。本篇基于 Android API 中的 AudioManager 作講述&#xff0c;最后給出實例。下面是本篇大綱&#xff1a;1、認識 AudioManage…

靜態創意和動態創意_再次發揮創意需要什么?

靜態創意和動態創意重點 (Top highlight)According to Oxford dictionary, creativity means “1. Inventiveness. 2. the use of imagination or original ideas to create something.”根據牛津詞典&#xff0c;創造力意味著“ 1。 創造力。 2.利用想象力或獨創性的思想來創造…

oracle 存儲過程 stored procedure 查詢一條記錄或多條記錄

創建基本表 -- Create table create table USER_INFORMATION ( P_ID NUMBER, USER_LOGIN_NAME NVARCHAR2(30) ) 創建包: create or replace package pack_test is type cur_test is ref cursor; end pack_test; / --這個不能少呀&#xff0c;加上這個就可以在…

我寫了 ahooks 源碼分析系列,收到官方邀請我一起維護,這是一次提 PR 的記錄...

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

Hdu 4415 Assassin's Creed 【貪心】.cpp

題意&#xff1a; 某A有一個劍 堅韌度為m 他可以用這個劍去攻打別的隊伍 殺掉第 i 個隊伍需要消耗的堅韌度為 Ai 并可以用得到的劍去打別的隊(Bi個) 但是打完別的隊這個劍就不能用了 問怎么用最少的堅韌度擊敗最多的隊伍 給出T組樣例 每個樣例給出n m n表示有n個隊 接下來n行給…

ahooks 整體架構篇,大家都能看得懂

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

gif動態圖gif出處_我喜歡GIF的怪異事物

gif動態圖gif出處I was recently reminded that I never wrote down all the weird things I learned about the GIF file format when implementing GIF decoding/playback at work last year. (I was reminded of this because I wrote a line in a corporate blog post draf…

C#字符串學習筆記

前言&#xff1a;記得我們老師說過一句話&#xff0c;對字符串的學習程度就是當別人打你一拳你知道痛的情況&#xff0c;所以字符串的處理我們必須學的差不多&#xff0c;這幾篇博客完全是我的學習過程中記錄的筆記&#xff0c;在這里分享一下讓很多剛開始學習.net編程的人能夠…

Git基礎教程(必學)

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

用戶體驗改善案例_優化用戶體驗案例研究的五種方法

用戶體驗改善案例重點 (Top highlight)I’ve had the opportunity to give several portfolio reviews, and I want to share some common themes I see and how you can improve them to put your best foot forward as you search for that new product design gig.我有機會發…

video from html5

掌握HTML5中的多媒體--視頻(video) 除非你一直生活在一個偏遠的島嶼上,過去一年左右的時間,你應該已經聽說過HTML5的各式炒作。HTML5將重塑富Web應用的未來。 下面 Figure 1的示例展示了HTML5中video標簽與傳統的object標簽的不同. Figure 1 1. <section> 2. <h…

我撿到寶了!2022版前端面試上岸手冊,最新最細致!

大裁員背景下&#xff0c;沒什么比辭職后找不到工作更扎心&#xff01;在行情好轉前&#xff0c;前端程序員只能“猥瑣發育”&#xff0c;不輕易跳槽&#xff0c;同時要修煉內功&#xff1a;對八股文、底層源碼、重點項目等進行查缺補漏&#xff0c;靜待行情好轉抓住機會&#…