Django - url 的动态视图

发布于 2024-11-08 12:47:47 字数 458 浏览 2 评论 0原文

我想根据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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

dawn曙光 2024-11-15 12:47:47

你可以做这样的事情。

#urls.py
url(r'^channel/(?P<channel>\d+)/

如果使用基于类的视图,switcher() 中的调用将如下所示:

#views.py
def switcher(request, channel):
    if channel == 'Whatever':
        return ViewForThisChannel.as_view()(request)  # <-- call to CBV

def ViewForThisChannel(View):
    #handle like a regular class-based view
, switcher, name='channel_render'), #views.py def switcher(request, channel): if channel == 'Whatever': return view_for_this_channel() def view_for_this_channel() #handle like a regular view

如果使用基于类的视图,switcher() 中的调用将如下所示:

You could do something like this.

#urls.py
url(r'^channel/(?P<channel>\d+)/

If using class-based views, the call in your switcher() will look like this:

#views.py
def switcher(request, channel):
    if channel == 'Whatever':
        return ViewForThisChannel.as_view()(request)  # <-- call to CBV

def ViewForThisChannel(View):
    #handle like a regular class-based view
, switcher, name='channel_render'), #views.py def switcher(request, channel): if channel == 'Whatever': return view_for_this_channel() def view_for_this_channel() #handle like a regular view

If using class-based views, the call in your switcher() will look like this:

踏月而来 2024-11-15 12:47:47

对于重定向,您应该使用 Django 重定向快捷方式功能:

from django.shortcuts import redirect

def my_view(request):
    ...
    return redirect('some-view-name', foo='bar')

https://docs .djangoproject.com/en/1.7/topics/http/shortcuts/#redirect

For redirecting you should use the Django redirect shortcut function:

from django.shortcuts import redirect

def my_view(request):
    ...
    return redirect('some-view-name', foo='bar')

https://docs.djangoproject.com/en/1.7/topics/http/shortcuts/#redirect

匿名的好友 2024-11-15 12:47:47

我认为最简单的方法是加载一个充当小型调度程序的视图,它调用您感兴趣的最终视图。

至于如何做到这一点,视图只是以特定方式调用的函数并期望返回特定的东西。您可以从一个视图调用另一个视图;只需确保您正确返回结果即可。

您可以使用 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.

尐籹人 2024-11-15 12:47:47

尝试像普通视图一样调用,例如

def configure_view(request, channel):
    print channel

url(r'^channel/(?P<channel>\d+)/
, configure_view, name='channel_render'),

try calling like a normal view e.g.

def configure_view(request, channel):
    print channel

url(r'^channel/(?P<channel>\d+)/
, configure_view, name='channel_render'),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文