国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

django views重定向到帶參數的url

瀏覽:5日期:2024-09-11 18:31:12

當一個函數進行完成后需要重定向到一個帶參數的url

URL

path(’peopleapply/<int:jobid>/’,second_views.peopleapply,name=’peopleapply’),函數

def peopleapply(request,jobid):略調用

return redirect(’peopleapply’,jobid = people.jbnum_id)

peopleapply 為函數再url中的name

jobid 為需要的參數名稱

補充:DJANGO中多種重定向方法使用

本文主要講解使用HttpResponseRedirect、redirect、reverse以及配置文件中配置URL等重定向方法

本文使用了Django1.8.2

使用場景,例如在表單一中提交數據后,需要返回到另一個指定的頁面即可使用重定向方法

一、 使用HttpResponseRedirect

fuhao The first argument to the constructor is required ? the path to redirect to. This can be a fully qualified URL or an absolute path with no domain。”參數可以是絕對路徑跟相對路徑”

from django.http import HttpResponseRedirect @login_required def update_time(request): #表單處理OR邏輯處理 return HttpResponseRedirect(’/’) #跳轉到主界面 #如果需要傳參數return HttpResponseRedirect(’/commons/index/?message=error’)二 redirect和reverse

from django.core.urlresolvers import reverse from django.shortcuts import redirect #https://docs.djangoproject.com/en/1.8.2/topics/http/shortcuts/ @login_required def update_time(request): #進行要處理的邏輯 return redirect(reverse(’test.views.invoice_return_index’, args=[])) #跳轉到index界面

redirect 類似HttpResponseRedirect的用法,也可以使用 字符串的url格式 /..index/?a=add reverse 可以直接用views函數來指定重定向的處理函數,args是url匹配的值。

三、 其他

#其他的也可以直接在url中配置from django.views.generic.simple import redirect_to 在url中添加 (r’^test/$’, redirect_to, {’url’: ’/author/’}), #我們甚至可以使用session的方法傳值request.session[’error_message’] = ’test’ redirect(’%s?error_message=test’ % reverse(’page_index’)) #這些方式類似于刷新,客戶端重新指定url。四、

#重定向,如果需要攜帶參數,那么能不能直接調用views中 url對應的方法來實現呢,默認指定一個參數。#例如view中有個方法baseinfo_account, 然后另一個url(對應view方法為blance_account)要重定向到這個baseinfo_account。#url中的配置:urlpatterns = patterns(’’, url(r’^index/’, ’account.views.index_account’), url(r’^blance/’, ’account.views.blance_account’), ) # views.py@login_required def index_account(request, args=None): ​#按照正常的url匹配這么寫有點不合適,看起來不規范 ​if args: print args return render(request, ’accountuserinfo.html’, {'user': user}) @login_required def blance_account(request): return index_account(request, {'name': 'orangleliu'}) #測試為:#1 直接訪問 /index 是否正常 (測試ok)#2 訪問 /blance 是否能正常的重定向到 /index頁面,并且獲取到參數(測試ok,頁面為/index但是瀏覽器地址欄的url仍然是/blance)#這樣的帶參數重定向是可行的。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Django
相關文章:
主站蜘蛛池模板: 花莲县| 璧山县| 吉木萨尔县| 宣城市| 祁东县| 出国| 饶河县| 青海省| 昭通市| 金阳县| 犍为县| 新余市| 玛曲县| 宜黄县| 禄劝| 游戏| 江西省| 定兴县| 册亨县| 东港市| 靖安县| 岢岚县| 荥阳市| 石屏县| 酉阳| 南安市| 宜昌市| 万源市| 普定县| 泸溪县| 汪清县| 壤塘县| 平塘县| 德清县| 石阡县| 泾阳县| 长春市| 潜江市| 九江县| 绥中县| 深泽县|