日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Django里自定义用户登陆及登陆后跳转到登陆前页面的实现

發布時間:2025/3/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Django里自定义用户登陆及登陆后跳转到登陆前页面的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

因為下一步要和公司的UM帳號作集成,所以分離出登陸模塊,及實現其基本功能是必不可少的。

登陸倒容易,但要實現在登陸后,跳轉到登陸前的網頁,且顯示用戶的登陸狀態,花了點時間查找代碼,

測試了五六種方式,終于搞定。連語法都OK啦。。

login.html:

{% extends "xxxx/index.html" %} {% load staticfiles %}{% block title %}用戶登陸 {% endblock %}{% block heading %}<h1>LOGIN </h2> {% endblock %}{% block content %}<div class="tm-middle"><div class="uk-container uk-container-center"><h3 class="tm-article-subtitle uk-margin-top">用戶登陸</h3><hr class="uk-grid-divider"><div class="uk-panel uk-panel-box uk-container-center uk-width-1-3"><h3 class="uk-panel-title"><i class="uk-icon-user"></i>用戶登陸</h3><div class="uk-width-3-3"><div><form action="{% url 'login' %}" class="uk-form uk-form-horizontal" role="form" method="post"><form method="post" action="./?next={{ redirect_to }}">{% csrf_token %}<div class="uk-form-row"><label for="id_username" class="uk-form-label" style="color: #e28327">用戶名:</label><div class="uk-form-controls"><input type="text" name="username" maxlength="100"autocapitalize="off"autocorrect="off" class="form-control textinput textInput"id="id_username" required{% if form.username.value %}value="{{ form.username.value }}"{% endif %}></div>{% if form.username.errors %}<p class="text-error">不正確的用戶名</p>{% endif %}</div><div class="uk-form-row uk-width-3-3"><label for="id_password" class="uk-form-label" style="color: #e28327">密碼:</label><div class="uk-form-controls"><input type="password" name="password" maxlength="100"autocapitalize="off" autocorrect="off" class="form-control textinput textInput"id="id_password" required>{% if form.password.errors %}<p class="text-error">不正確的用戶名或密碼</p>{% endif %}</div></div><input type="hidden" name="next" value="{{ request.GET.next }}">{% if form.non_field_errors %}{% for error in form.non_field_errors %}<div class="well well-small text-error" style="border: none; color: red">請輸入正確的用戶名和密碼</div>{% endfor %}{% endif %}<button type="submit" class="uk-button uk-button-success uk-margin-top uk-float-right" id="submit-id-submit">登錄</button></form></div></div></div></div></div><div style="height:100px"></div>{% endblock %}

view.py:

from django.contrib.auth import authenticate, login, logout
@csrf_exempt
def user_login(request):redirect_to = request.REQUEST.get('next', '')if request.method == 'POST':username = request.POST.get('username', '')password = request.POST.get('password', '')user = authenticate(username=username, password=password)if user and user.is_active:login(request, user)return HttpResponseRedirect(request.POST.get('next', '/') or '/')return render_to_response("xxxx/login.html", locals(), RequestContext(request))

urls.py:

url(r'^accounts/login/$', user_login, name='login'),

?

轉載于:https://www.cnblogs.com/aguncn/p/5557011.html

總結

以上是生活随笔為你收集整理的Django里自定义用户登陆及登陆后跳转到登陆前页面的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。