因为下一步要和公司的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'),
时间: 2024-12-04 21:52:01