欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

django跳轉(zhuǎn)頁(yè)面?zhèn)鲄⒌膶?shí)現(xiàn)

 更新時(shí)間:2020年09月17日 10:47:51   作者:xsan  
這篇文章主要介紹了django跳轉(zhuǎn)頁(yè)面?zhèn)鲄⒌膶?shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、情景

    eg:查看一條數(shù)據(jù)的詳情,需要跳轉(zhuǎn)頁(yè)面,并進(jìn)行傳值

二、思路

方式1:觸發(fā)詳情按鈕時(shí),Js獲取到該條數(shù)據(jù)的id值,并傳遞給url,后臺(tái)接受到該請(qǐng)求,通過(guò)id查詢(xún)到這條數(shù)據(jù)。并返回一個(gè)json串給前端。前端拿到數(shù)據(jù)進(jìn)行處理,映射給頁(yè)面。

方式2:觸發(fā)詳情按鈕時(shí),同時(shí)前端進(jìn)行本地保存當(dāng)前數(shù)據(jù)(sessionstorage\localstorage),跳轉(zhuǎn)頁(yè)面后,前端直接從storage當(dāng)前取值并回顯。

           ①關(guān)于數(shù)據(jù)存儲(chǔ):

            sessionstorage:數(shù)據(jù)存儲(chǔ),關(guān)閉窗口的同時(shí),清除數(shù)據(jù)

            localstorage:數(shù)據(jù)存儲(chǔ),未定義過(guò)期時(shí)間,一直存在本地

需要注意的是:當(dāng)前端頁(yè)面發(fā)生跳轉(zhuǎn)時(shí),資源都會(huì)被重載,當(dāng)未進(jìn)行傳值的情況下,無(wú)法跨頁(yè)面加載數(shù)據(jù)。

三 實(shí)現(xiàn):

方式1:URL傳值

①獲取id后直接傳遞給URL

window.location.href=`index.html?nid=${id}`;

②再跳轉(zhuǎn)到index.html的js中獲取到該并id解析

(function() {
 window.onload = function() {
    var url=window.location.href;
    var url_param = url.split("?")[1];
    var url_param_arr = url_param.split("=");
    var nid ={nid:url_param_arr[1]};
    preview_index(nid);//處理函數(shù),發(fā)送請(qǐng)求
 }
})();

方式2:本地存儲(chǔ)

①存

//本地存儲(chǔ)
        var storage = window.sessionStorage;
        storage['index_name'] = $('#index_name').val();
        storage['index_title'] = $('#index_title').val();
        storage['index_content'] = $('#index_content').val();

②讀取

var storage=window.sessionStorage;
title=storage.index_title

四、其他方法

1、如果在反轉(zhuǎn)url的時(shí)候,需要添加參數(shù),那么可以通過(guò)傳遞'kwargs'參數(shù)到'reverse'函數(shù)中。實(shí)例代碼:

urls.py

from django.urls import path, re_path
from app01 import views

urlpatterns = [
  path('article/<year>/<month>/',views.article,name='article'),
  path('', views.Login.as_view(), name="login"),
  ]

views.py

from django.shortcuts import HttpResponse, redirect, reverse
from django.contrib.auth.models import User, 
from django.views.generic import View
from django.contrib.auth import authenticate, login,

class Login(View):
  def get(self, request):
    return render(request, 'login.html')

  def post(self, request):
    username = request.POST.get('username')
    passwd = request.POST.get('passwd')
    user = authenticate(request, username=username, password=passwd)
    if user is not None:
      if user.is_active:
        login(request, user)
        # 登錄成功跳轉(zhuǎn)頁(yè)面
        return redirect(reverse('article', kwargs={'year': 2019, 'month': 12}))
      else:
        err_msg = '用戶(hù)未激活,請(qǐng)聯(lián)系管理員進(jìn)行激活'
    else:
      err_msg = '用戶(hù)名或密碼有誤'
    return render(request, 'login.html', {"err_msg": err_msg, "username": username})


def article(request, year, month):
  return HttpResponse('您查詢(xún)的文章日期是:%s年%s月' %(year, month))

2、如果想要添加查詢(xún)字符串的參數(shù),則必須手動(dòng)的進(jìn)行拼接。實(shí)例代碼如下:

login_url = reverse('login')+"?next=/"

到此這篇關(guān)于django跳轉(zhuǎn)頁(yè)面?zhèn)鲄⒌膶?shí)現(xiàn)的文章就介紹到這了,更多相關(guān)django跳轉(zhuǎn)頁(yè)面?zhèn)鲄?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論