怎么在Django框架中使用內(nèi)置方法實(shí)現(xiàn)一個(gè)登錄功能-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)怎么在Django框架中使用內(nèi)置方法實(shí)現(xiàn)一個(gè)登錄功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

成都創(chuàng)新互聯(lián)公司專注于孝昌網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供孝昌營(yíng)銷(xiāo)型網(wǎng)站建設(shè),孝昌網(wǎng)站制作、孝昌網(wǎng)頁(yè)設(shè)計(jì)、孝昌網(wǎng)站官網(wǎng)定制、小程序開(kāi)發(fā)服務(wù),打造孝昌網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供孝昌網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。

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

怎么在Django框架中使用內(nèi)置方法實(shí)現(xiàn)一個(gè)登錄功能

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

1 位置

怎么在Django框架中使用內(nèi)置方法實(shí)現(xiàn)一個(gè)登錄功能

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)

三 實(shí)戰(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 因?yàn)槟J(rèn)的模板位置為registration/login.html,因此我們創(chuàng)建該文檔如下:

{% extends "base.html" %}
{% block title %}登錄{% endblock %}
{% block content %}
<div class="row text-center vertical-middle-sm">
 <h2>登錄</h2>
 <p>請(qǐng)輸入用戶名和密碼</p>
  <!--用具體的URL指明了數(shù)據(jù)的POST目標(biāo)-->
 <form class="form-horizontal" action="{% url 'account:user_login' %}" method="post">
 {% csrf_token %}
    <!--每個(gè)表單元素在一對(duì)P標(biāo)簽內(nèi)-->
    <!--{{ form.as_p }}-->
    <!--使用Bootstrap樣式使得表單更美麗-->
    <div class="form-group">
  <label for="{{ form.username.id_for_label }}" class="col-md-5 control-label" ><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" ><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/頁(yè)面
LOGIN_REDIRECT_URL = '/blog/'

4 測(cè)試

怎么在Django框架中使用內(nèi)置方法實(shí)現(xiàn)一個(gè)登錄功能

怎么在Django框架中使用內(nèi)置方法實(shí)現(xiàn)一個(gè)登錄功能

四 實(shí)戰(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"}),
]

看完上述內(nèi)容,你們對(duì)怎么在Django框架中使用內(nèi)置方法實(shí)現(xiàn)一個(gè)登錄功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

當(dāng)前名稱:怎么在Django框架中使用內(nèi)置方法實(shí)現(xiàn)一個(gè)登錄功能-創(chuàng)新互聯(lián)
URL標(biāo)題:http://www.muchs.cn/article8/dsoeip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、建站公司、Google、品牌網(wǎng)站設(shè)計(jì)面包屑導(dǎo)航、網(wǎng)站營(yíng)銷(xiāo)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)