在django中面临用户配置文件url方案(如example.com/username)的问题 [英] Facing problem with user profile url scheme like example.com/username in django

查看:87
本文介绍了在django中面临用户配置文件url方案(如example.com/username)的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在django应用程序中,我需要使用以下结构创建twitter用户个人资料网址:

In a django app, I need to create twitter user profile urls with following structure like:


  • 示例.com /< username>

  • example.com/<username>/friends

  • example.com/<username>/blog

  • 示例.com /< username> / some-page

  • example.com/<username>/some-other-page

  • example.com/<username>
  • example.com/<username>/friends
  • example.com/<username>/blog
  • example.com/<username>/some-page
  • example.com/<username>/some-other-page

我的urls.py:

urlpatterns = patterns('profiles.views',
    url(r'^(?P<account_name>[a-zA-Z0-0_.-]+)/$', 'show_profile', name='profiles_show_profile'),
    url(r'^(?P<account_name>[a-zA-Z0-0_.-]+)/friends/$', 'show_friends', name='profiles_show_blog'),
    url(r'^(?P<account_name>[a-zA-Z0-0_.-]+)/blog/$', 'show_blog', name='profiles_show_blog'),
)

我的第一个问题是,虽然 example.com /<使用rname> 工作正常 example.com/<username>/any-other-page 不。他们都以 show_profile 视图而不是自己的视图。

My first problem is that while example.com/<username> works fine example.com/<username>/any-other-page does not. They all end up at show_profile view instead of their own view.

注意:如果我将网址更改为 example.com/user/<用户名>

Note: Everything works fine if I make urls change the url structure to example.com/user/<username>

我在这里做错什么?请指教。

What am I doing wrong here? Please advise.

其次,我想在django最佳做法(陷阱,陷阱等)中处理url方案的指导,其中第一部分本身是可变的。

Secondly, I would like guidance on django best practices(pitfalls, gotchas etc) in dealing with url schemes where first part is itself is variable.

谢谢

推荐答案

我不明白为什么你的网址不起作用您可以尝试将第一个模式移动到最后,以便其他模式有机会先匹配。您所描述的问题听起来像 example.com/user/any-page ,该模式是匹配用户/任何页面作为帐户名称。你显示的正则表达式不会这样做,但也许你的实际代码稍有不同?

I don't see why your URLs aren't working. You can try moving the first pattern to the end, so that other patterns have a chance to match first. The problem you're describing sounds like example.com/user/any-page, the pattern is matching "user/any-page" as the account name. The regex you show wouldn't do that, but maybe your actual code is slightly different?

这篇关于在django中面临用户配置文件url方案(如example.com/username)的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆