欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

千鋒扣丁學堂Python培訓之Django框架使用內(nèi)置方法實現(xiàn)登錄功能詳解

2019-06-12 13:32:43 5044瀏覽

今天千鋒扣丁學堂Python培訓老師給大家分享一篇關(guān)于Django框架使用內(nèi)置方法實現(xiàn)登錄功能,結(jié)合實例形式詳細分析了Django框架內(nèi)置方法實現(xiàn)登錄功能的相關(guān)操作技巧與使用注意事項,下面我們一起來看一下吧。

一、內(nèi)置登錄退出思維導圖



二、Django內(nèi)置登錄方法

1、位置



2、源碼

@deprecate_current_app
@sensitive_post_parameters()
@csrf_protect
@never_cache
# 視圖函數(shù)要渲染的模板位置(registration/login.html)
def login(request, template_name='registration/login.html',
     redirect_field_name=REDIRECT_FIELD_NAME,
     authentication_form=AuthenticationForm,
     extra_context=None, redirect_authenticated_user=False):
  """
  Displays the login form and handles the login action.
  """
  redirect_to = request.POST.get(redirect_field_name, request.GET.get(redirect_field_name, ''))
  if redirect_authenticated_user and request.user.is_authenticated:
    redirect_to = _get_login_redirect_url(request, redirect_to)
    if redirect_to == request.path:
      raise ValueError(
        "Redirection loop for authenticated user detected. Check that "
        "your LOGIN_REDIRECT_URL doesn't point to a login page."
      )
    return HttpResponseRedirect(redirect_to)
  elif request.method == "POST":
    form = authentication_form(request, data=request.POST)
    if form.is_valid():
      auth_login(request, form.get_user())
      return HttpResponseRedirect(_get_login_redirect_url(request, redirect_to))
  else:
    form = authentication_form(request)
  current_site = get_current_site(request)
  context = {
    'form': form,
    redirect_field_name: redirect_to,
    'site': current_site,
    'site_name': current_site.name,
  }
  if extra_context is not None:
    context.update(extra_context)
  return TemplateResponse(request, template_name, context)


三、實戰(zhàn)一

1、編輯mysite/account/urls.py

from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
  # 自定義登錄
  # url(r'^login/$', views.user_login, name='user_login'),
  # django內(nèi)置的登錄
  url(r"^login/$", auth_views.login, name="user_login"),
]

2、因為默認的模板位置為registration/login.html,因此我們創(chuàng)建該文檔如下:

{% extends "base.html" %}
{% block title %}登錄{% endblock %}
{% block content %}
<div class="row text-center vertical-middle-sm">
 <h1>登錄</h1>
 <p>請輸入用戶名和密碼</p>
  <!--用具體的URL指明了數(shù)據(jù)的POST目標-->
 <form class="form-horizontal" action="{% url 'account:user_login' %}" method="post">
 {% csrf_token %}
    <!--每個表單元素在一對P標簽內(nèi)-->
    <!--{{ form.as_p }}-->
    <!--使用Bootstrap樣式使得表單更美麗-->
    <div class="form-group">
  <label for="{{ form.username.id_for_label }}" class="col-md-5 control-label" style="color:red"><span class="glyphicon glyphicon-user"></span>Username</label>
  <div class="col-md-6 text-left">{{ form.username }}</div>
 </div>
 <div class="form-group">
  <label for="{{ form.password.id_for_label }}" class="col-md-5 control-label" style="color:blue"><span class="glyphicon glyphicon-floppy-open"></span>Password</label>
  <div class="col-md-6 text-left">{{ form.password }}</div>
 </div>
 <input type="submit" value="Login">
 </form>
</div>
{% endblock %}

3、修改mysite/mysite/settings.py

# 登錄后重定向到http://localhost:8000/blog/頁面
LOGIN_REDIRECT_URL = '/blog/'

4、測試



四、實戰(zhàn)二

1、編輯mysite/account/urls.py

from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
  # 自定義登錄
  # url(r'^login/$', views.user_login, name='user_login'),
  # django內(nèi)置的登錄
  url(r"^login/$", auth_views.login, name="user_login"),
  url(r"^new-login/$", auth_views.login, {"template_name": "account/login.html"}),
]

2、測試



以上就是關(guān)于千鋒扣丁學堂Python培訓之Django框架使用內(nèi)置方法實現(xiàn)登錄功能詳解的全部內(nèi)容,想要了解更多關(guān)于Python開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學堂Python培訓官網(wǎng)、微信等平臺,扣丁學堂IT職業(yè)在線學習教育有專業(yè)的Python講師為您指導,此外扣丁學堂老師精心推出的Python視頻教程定能讓你快速掌握Python從入門到精通開發(fā)實戰(zhàn)技能??鄱W堂Python技術(shù)交流群:279521237。


扣丁學堂微信公眾號                          Python全棧開發(fā)爬蟲人工智能機器學習數(shù)據(jù)分析免費公開課直播間


      【關(guān)注微信公眾號獲取更多學習資料】         【掃碼進入Python全棧開發(fā)免費公開課】



查看更多關(guān)于"Python開發(fā)資訊"的相關(guān)文章>

標簽: Python培訓 Python視頻教程 Python在線視頻 Python學習視頻 Python培訓班

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部