Django中傳遞參數(shù)到URLconf的視圖函數(shù)中的方法
有時你會發(fā)現(xiàn)你寫的視圖函數(shù)是十分類似的,只有一點點的不同。 比如說,你有兩個視圖,它們的內(nèi)容是一致的,除了它們所用的模板不太一樣:
# urls.py from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^foo/$', views.foo_view), (r'^bar/$', views.bar_view), ) # views.py from django.shortcuts import render_to_response from mysite.models import MyModel def foo_view(request): m_list = MyModel.objects.filter(is_new=True) return render_to_response('template1.html', {'m_list': m_list}) def bar_view(request): m_list = MyModel.objects.filter(is_new=True) return render_to_response('template2.html', {'m_list': m_list})
我們在這代碼里面做了重復(fù)的工作,不夠簡練。 起初你可能會想,通過對兩個URL都使用同樣的視圖,在URL中使用括號捕捉請求,然后在視圖中檢查并決定使用哪個模板來去除代碼的冗余,就像這樣:
# urls.py from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^(foo)/$', views.foobar_view), (r'^(bar)/$', views.foobar_view), ) # views.py from django.shortcuts import render_to_response from mysite.models import MyModel def foobar_view(request, url): m_list = MyModel.objects.filter(is_new=True) if url == 'foo': template_name = 'template1.html' elif url == 'bar': template_name = 'template2.html' return render_to_response(template_name, {'m_list': m_list})
這種解決方案的問題還是老缺點,就是把你的URL耦合進你的代碼里面了。 如果你打算把 /foo/ 改成 /fooey/ 的話,那么你就得記住要去改變視圖里面的代碼。
對一個可選URL配置參數(shù)的優(yōu)雅解決方法: URLconf里面的每一個模式都可以包含第三個數(shù)據(jù): 一個關(guān)鍵字參數(shù)的字典:
有了這個概念以后,我們就可以把我們現(xiàn)在的例子改寫成這樣:
# urls.py from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^foo/$', views.foobar_view, {'template_name': 'template1.html'}), (r'^bar/$', views.foobar_view, {'template_name': 'template2.html'}), ) # views.py from django.shortcuts import render_to_response from mysite.models import MyModel def foobar_view(request, template_name): m_list = MyModel.objects.filter(is_new=True) return render_to_response(template_name, {'m_list': m_list})
如你所見,這個例子中,URLconf指定了 template_name 。 而視圖函數(shù)會把它當(dāng)成另一個參數(shù)。
這種使用額外的URLconf參數(shù)的技術(shù)以最小的代價給你提供了向視圖函數(shù)傳遞額外信息的一個好方法。
相關(guān)文章
Python WEB應(yīng)用部署的實現(xiàn)方法
這篇文章主要介紹了Python WEB應(yīng)用部署的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01python實現(xiàn)合并多個list及合并多個django QuerySet的方法示例
這篇文章主要介紹了python實現(xiàn)合并多個list及合并多個django QuerySet的方法,結(jié)合實例形式分析了Python使用chain合并多個list以及合并Django中多個QuerySet的相關(guān)操作技巧,需要的朋友可以參考下2019-06-06python實現(xiàn)的讀取網(wǎng)頁并分詞功能示例
這篇文章主要介紹了python實現(xiàn)的讀取網(wǎng)頁并分詞功能,結(jié)合實例形式分析了Python使用requests模塊讀取網(wǎng)頁,以及jieba庫分詞的相關(guān)操作技巧,需要的朋友可以參考下2019-10-10一文教會你利用Python程序讀取Excel創(chuàng)建折線圖
不同類型的圖表有不同的功能,柱形圖主要用于對比數(shù)據(jù),折線圖主要用于展示數(shù)據(jù)變化的趨勢,散點圖主要用于判斷數(shù)據(jù)的相關(guān)性,下面這篇文章主要給大家介紹了關(guān)于如何通過一文教你利用Python程序讀取Excel創(chuàng)建折線圖的相關(guān)資料,需要的朋友可以參考下2022-11-11Django使用HttpResponse返回圖片并顯示的方法
今天小編就為大家分享一篇Django使用HttpResponse返回圖片并顯示的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05python爬蟲之BeautifulSoup 使用select方法詳解
本篇文章主要介紹了python爬蟲之BeautifulSoup 使用select方法詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10python字符串,元組,列表,字典互轉(zhuǎn)代碼實例詳解
這篇文章主要介紹了python字符串,元組,列表,字典互轉(zhuǎn)代碼實例詳解,需要的朋友可以參考下2020-02-02