Django 多个视图参数和重复的命名 url

发布于 2024-08-09 19:39:42 字数 431 浏览 3 评论 0原文

我只是想知道使两个命名网址相同是否会产生任何问题。我尝试过并且有效。 例如,我有一个能够进行分页的视图:

def info(request, page_num = 1)    

并且我想以两种方式调用它,如:

/info
/info/page/1

所以我制作了如下网址:

url(r'^info/$', 'views.info', name='info'),
url(r'^info/(?P<page_num>)\d+)/$', 'views.info', name='info'),

并且它似乎有效。有什么问题吗,或者我应该以不同的方式命名我的第二个网址,例如 info_pagination

I was just pondering if make two named urls the same produces any problems. I tried it and it works.
So for example, I have a view that is able to do paging:

def info(request, page_num = 1)    

and I would like to call it both ways, as:

/info
/info/page/1

so I made urls like:

url(r'^info/

and it seems to work. Anything wrong with that, or should I name my second url differently, like info_paginated for example.

, 'views.info', name='info'), url(r'^info/(?P<page_num>)\d+)/

and it seems to work. Anything wrong with that, or should I name my second url differently, like info_paginated for example.

, 'views.info', name='info'),

and it seems to work. Anything wrong with that, or should I name my second url differently, like info_paginated for example.

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

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

发布评论

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

评论(2

清旖 2024-08-16 19:39:42

据我所知,这没有什么问题,而且是相当标准的做法。我知道我会这样做,主要是当我使用与 Javascript 代码集成的 URL 并且我没有在页面加载时使用的参数时。

Nothing wrong with that and as far as I know pretty standard practice. I know I do it, mostly when I'm using URL's that integrate with Javascript code and I don't have the parameter to use on page load.

自演自醉 2024-08-16 19:39:42

这完全没问题,但您可能需要设置 page_num = None

if page_num is None:
    return redirect(reverse('info',{'page_num':1})

因为搜索引擎会看到 yoursite.com/info 和 yoursite.com/info/1 并认为它们是重复的内容,如果您从一个重定向到另一个你会没事的。

It is perfectly fine, but you might want to set page_num = None, then

if page_num is None:
    return redirect(reverse('info',{'page_num':1})

because a search engine sees yoursite.com/info and yoursite.com/info/1 and thinks they are duplicated content, if you redirect from one to another you will be fine.

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