如何在 Django URLpatterns 中调度请求方法?

发布于 2024-09-03 20:02:21 字数 446 浏览 4 评论 0 原文

很清楚如何创建从 URL 正则表达式分派的 URLPattern:

(r'^books/$', books),

其中 books 可以根据请求方法进一步分派:

def books(request):
    if request.method == 'POST':
        ...
    else:
        ...

我想知道是否有一种惯用的方法将请求方法包含在 URLPattern 中,将所有调度/路线信息保存在一个位置,例如:

(r'^books/$', GET, retrieve-book),
(r'^books/$', POST, update-books),
(r'^books/$', PUT, create-books),

It's clear how to create a URLPattern which dispatches from a URL regex:

(r'^books/

where books can further dispatch on request method:

def books(request):
    if request.method == 'POST':
        ...
    else:
        ...

I'd like to know if there is an idiomatic way to include the request method inside the URLPattern, keeping all dispatch/route information in a single location, such as:

(r'^books/
, books),

where books can further dispatch on request method:


I'd like to know if there is an idiomatic way to include the request method inside the URLPattern, keeping all dispatch/route information in a single location, such as:


, GET, retrieve-book),
(r'^books/
, books),

where books can further dispatch on request method:


I'd like to know if there is an idiomatic way to include the request method inside the URLPattern, keeping all dispatch/route information in a single location, such as:


, POST, update-books),
(r'^books/
, books),

where books can further dispatch on request method:


I'd like to know if there is an idiomatic way to include the request method inside the URLPattern, keeping all dispatch/route information in a single location, such as:


, PUT, create-books),

, books),

where books can further dispatch on request method:


I'd like to know if there is an idiomatic way to include the request method inside the URLPattern, keeping all dispatch/route information in a single location, such as:


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

澜川若宁 2024-09-10 20:02:22

它作为单一视图方法完成的原因是您通常呈现某种页面内容作为您要提交的表单的上下文。

不管怎样,我回复它的原因是:从你的示例 URLConf 来看,你似乎正在使用 Django 构建一个 REST Web 服务——如果是这样的话,你可能真的会从使用相当好的 django-piston 自动创建您的资源/集合。它使用基于类的处理程序,根据请求 UPDATE 中的 HTTP 方法(四年后!)自动重定向到适当的方法(在您的情况下为 get-books、update-books、create-books

),而 django-piston 仍然存在(并且有效),Django REST Framework 是一个更复杂、有文档记录和扩展的选择,这些天。

The reason it's done as a single view method is that you're usually rendering some kind of page content as context for the form you're about to submit.

Anyway, my reason for replying it this: from your sample URLConf there it looks like you're building a REST webservice with Django -- if this is the case, you might really benefit from using the rather good django-piston to automatically create your resources/collections. It uses class-based handlers that automatically redirect to the appropriate method (get-books, update-books, create-books in your case) based on the HTTP method in the request

UPDATE (four years later!) while django-piston still exists (and works), Django REST Framework is a much more sophisticated, documented and extended choice these days.

谈下烟灰 2024-09-10 20:02:22

除了您在第二个代码片段中使用的内容之外,标准 Django 没有任何区分请求方法的机制:

if request.method == 'POST':
    ...

但是,有第三方应用程序和代码片段尝试使用基于类的视图使方法处理更加干净。例如,请参阅此代码段(可从 这个关于类视图的问题)。

就我个人而言,我不太确定这是一个好主意。标准的 Django 方法是如此……标准……我认为这会在实际上不需要的地方引入额外的混乱和复杂性。

Standard Django doesn't have any mechanism for differentiating request methods besides what you used in your second snippet:

if request.method == 'POST':
    ...

However, there are third-party apps and snippets that attempt to make method handling a little cleaner using class based views. See, for example, this snippet (found from this SO question about class views).

Personally I'm not so sure this is a good idea. The standard Django method is so... standard... that I think this introduces extra confusion and complexity where it really isn't needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文