django跳轉(zhuǎn)頁面?zhèn)鲄⒌膶崿F(xiàn)
一、情景
eg:查看一條數(shù)據(jù)的詳情,需要跳轉(zhuǎn)頁面,并進(jìn)行傳值
二、思路
方式1:觸發(fā)詳情按鈕時,Js獲取到該條數(shù)據(jù)的id值,并傳遞給url,后臺接受到該請求,通過id查詢到這條數(shù)據(jù)。并返回一個json串給前端。前端拿到數(shù)據(jù)進(jìn)行處理,映射給頁面。
方式2:觸發(fā)詳情按鈕時,同時前端進(jìn)行本地保存當(dāng)前數(shù)據(jù)(sessionstorage\localstorage),跳轉(zhuǎn)頁面后,前端直接從storage當(dāng)前取值并回顯。
①關(guān)于數(shù)據(jù)存儲:
sessionstorage:數(shù)據(jù)存儲,關(guān)閉窗口的同時,清除數(shù)據(jù)
localstorage:數(shù)據(jù)存儲,未定義過期時間,一直存在本地
需要注意的是:當(dāng)前端頁面發(fā)生跳轉(zhuǎn)時,資源都會被重載,當(dāng)未進(jìn)行傳值的情況下,無法跨頁面加載數(shù)據(jù)。
三 實現(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ā)送請求
}
})();
方式2:本地存儲
①存
//本地存儲
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ù),那么可以通過傳遞'kwargs'參數(shù)到'reverse'函數(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)頁面
return redirect(reverse('article', kwargs={'year': 2019, 'month': 12}))
else:
err_msg = '用戶未激活,請聯(lián)系管理員進(jìn)行激活'
else:
err_msg = '用戶名或密碼有誤'
return render(request, 'login.html', {"err_msg": err_msg, "username": username})
def article(request, year, month):
return HttpResponse('您查詢的文章日期是:%s年%s月' %(year, month))
2、如果想要添加查詢字符串的參數(shù),則必須手動的進(jìn)行拼接。實例代碼如下:
login_url = reverse('login')+"?next=/"
到此這篇關(guān)于django跳轉(zhuǎn)頁面?zhèn)鲄⒌膶崿F(xiàn)的文章就介紹到這了,更多相關(guān)django跳轉(zhuǎn)頁面?zhèn)鲄?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python工具模塊介紹之time?時間訪問和轉(zhuǎn)換的示例代碼
這篇文章主要介紹了python工具模塊介紹-time?時間訪問和轉(zhuǎn)換,本文通過示例代碼給大家介紹的非常詳細(xì),對大家啊的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
python抓取某汽車網(wǎng)數(shù)據(jù)解析html存入excel示例
python抓取某汽車網(wǎng)經(jīng)銷商信息網(wǎng)頁數(shù)據(jù)解析html,這里提供一個示例演示,大家可以根據(jù)需要分析自己網(wǎng)站的數(shù)據(jù)2013-12-12
python Django編寫接口并用Jmeter測試的方法
這篇文章主要介紹了python Django編寫接口并用Jmeter測試,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python基于matplotlib畫箱體圖檢驗異常值操作示例【附xls數(shù)據(jù)文件下載】
這篇文章主要介紹了Python基于matplotlib畫箱體圖檢驗異常值操作,涉及Python針對xls格式數(shù)據(jù)文件的讀取、matplotlib圖形繪制等相關(guān)操作技巧,并附帶xls數(shù)據(jù)文件供讀者下載參考,需要的朋友可以參考下2019-01-01

