Django里自定义用户登陆及登陆后跳转到登陆前页面的实现
生活随笔
收集整理的這篇文章主要介紹了
Django里自定义用户登陆及登陆后跳转到登陆前页面的实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
因?yàn)橄乱徊揭凸镜腢M帳號(hào)作集成,所以分離出登陸模塊,及實(shí)現(xiàn)其基本功能是必不可少的。
登陸倒容易,但要實(shí)現(xiàn)在登陸后,跳轉(zhuǎn)到登陸前的網(wǎng)頁(yè),且顯示用戶的登陸狀態(tài),花了點(diǎn)時(shí)間查找代碼,
測(cè)試了五六種方式,終于搞定。連語(yǔ)法都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">請(qǐng)輸入正確的用戶名和密碼</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'),?
轉(zhuǎn)載于:https://www.cnblogs.com/aguncn/p/5557011.html
總結(jié)
以上是生活随笔為你收集整理的Django里自定义用户登陆及登陆后跳转到登陆前页面的实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux下x86_64进程地址空间布局
- 下一篇: (计算机组成原理)第二章数据的表示和运算