Django - url 的动态视图
我想根据url加载特定的视图,例如:
url(r'^channel/(?P<channel>\d+)/$', ---, name='channel_render'),
根据传入url的通道,我想加载特定的视图文件。我尝试这样做:
def configure_view(channel):
print channel
urlpatterns = patterns('',
url(r'^channel/(?P<channel>\d+)/$', configure_view(channel), name='channel_render'),
但显然频道参数没有被传入。有什么办法可以做到这一点吗?我能想到的唯一其他解决方案是加载管理器视图,然后从那里加载相关的视图文件。如果这是唯一的方法,如何从视图内重定向到另一个视图文件?
I want to load a particular view depending on the url, for example:
url(r'^channel/(?P<channel>\d+)/
Depending on the channel passed into the url, I want to load a specific view file. I tried doing this:
def configure_view(channel):
print channel
urlpatterns = patterns('',
url(r'^channel/(?P<channel>\d+)/
But obviously the channel argument is not getting passed in. Is there any way to do this? The only other solution I can think of is loading a manager view and then loading the relevant view file from there. If this is the only way, how do I redirect to another view file from within a view?
, ---, name='channel_render'),
Depending on the channel passed into the url, I want to load a specific view file. I tried doing this:
But obviously the channel argument is not getting passed in. Is there any way to do this? The only other solution I can think of is loading a manager view and then loading the relevant view file from there. If this is the only way, how do I redirect to another view file from within a view?
, configure_view(channel), name='channel_render'),
But obviously the channel argument is not getting passed in. Is there any way to do this? The only other solution I can think of is loading a manager view and then loading the relevant view file from there. If this is the only way, how do I redirect to another view file from within a view?
, ---, name='channel_render'),Depending on the channel passed into the url, I want to load a specific view file. I tried doing this:
But obviously the channel argument is not getting passed in. Is there any way to do this? The only other solution I can think of is loading a manager view and then loading the relevant view file from there. If this is the only way, how do I redirect to another view file from within a view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以做这样的事情。
如果使用基于类的视图,
switcher()
中的调用将如下所示:You could do something like this.
If using class-based views, the call in your
switcher()
will look like this:对于重定向,您应该使用 Django 重定向快捷方式功能:
https://docs .djangoproject.com/en/1.7/topics/http/shortcuts/#redirect
For redirecting you should use the Django redirect shortcut function:
https://docs.djangoproject.com/en/1.7/topics/http/shortcuts/#redirect
我认为最简单的方法是加载一个充当小型调度程序的视图,它调用您感兴趣的最终视图。
至于如何做到这一点,视图只是以特定方式调用的函数并期望返回特定的东西。您可以从一个视图调用另一个视图;只需确保您正确返回结果即可。
您可以使用
import
从不同文件加载视图。I think the easiest way to do this is to load a view that functions as a tiny dispatcher, which calls the final view you're interested in.
As far as how to do that, views are just functions that get called in a particular way and expected to return a particular thing. You can call one view from another; just make sure you're properly returning the result.
You can load views from different files with
import
.尝试像普通视图一样调用,例如
try calling like a normal view e.g.