Django 多个视图参数和重复的命名 url
我只是想知道使两个命名网址相同是否会产生任何问题。我尝试过并且有效。 例如,我有一个能够进行分页的视图:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,这没有什么问题,而且是相当标准的做法。我知道我会这样做,主要是当我使用与 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.
这完全没问题,但您可能需要设置
page_num = None
,因为搜索引擎会看到 yoursite.com/info 和 yoursite.com/info/1 并认为它们是重复的内容,如果您从一个重定向到另一个你会没事的。
It is perfectly fine, but you might want to set
page_num = None
, thenbecause 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.