Tim*_*s83 1 django django-templates django-urls django-views
我有两个网址:
url(r'^(?P<slug>[^\.]+)/reviews/$', Someview.as_view(), name="someview"),
url(r'^(?P<slug>[^\.]+)/tips/$', Someview.as_view(), name="someview")
Run Code Online (Sandbox Code Playgroud)
我需要根据url的那一部分获取reviews或tips参与视图以在同一视图中应用不同的逻辑,我该怎么做?
你可以这样做:
url(r'^(?P<slug>[^\.]+)/(?P<reviews>reviews)/$', Someview.as_view(), name="someview"),
url(r'^(?P<slug>[^\.]+)/(?P<tips>tips)/$', Someview.as_view(), name="someview")
在视图中,您可以得到它们:
基于类的视图:
class SomeView(ListView):
def my_fn(self):
reviews = self.kwargs.get('reviews', None):
...
Run Code Online (Sandbox Code Playgroud)
基于功能的视图:
def SomeView(request, reviews):
#you can use it in the view now
revews = reviews
...
Run Code Online (Sandbox Code Playgroud)
您可以通过在URL中传递字典来获取更多变量.
url(r'^(?P<slug>[^\.]+)/reviews/$', Someview.as_view(), {'reviews':'reviews','tips':'tips'}, name="someview")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1205 次 |
| 最近记录: |