Django:如何从 urls.py.. 中的同一文件添加其他 url 模式?

标签 django django-urls

我已经看过文档 here我正在尝试做同样的事情,但它不起作用。它与网址不匹配。

这是我的 urls.py

profile_patterns = patterns('',
    url(r'^profile/$', views.profile),
    url(r'^thoughts/$', views.thoughts),
)

urlpatterns = patterns('',      
    url(r'^/user/(?P<username>\S+)/', include(profile_patterns)),

    # I also tried to do it this way.. But it isn't working.
    url(r'^abc/', include(patterns('', 
        url(r'^hello/$', views.profile)))),

我尝试访问以下 url。

'http://<mysite>.com/user/<someUsername>/profile/'
'http://<mysite>.com/abc/hello/'

最佳答案

试试这个 \w+ 而不是 \S+ 而不是 '' 而是用户 View 的路径:

profile_patterns = patterns('userapp.views',
  url(r'^profile/$', profile),
  url(r'^thoughts/$', thoughts),
) 

urlpatterns = patterns('',      
  url(r'^/user/(?P<username>\w+)/', include(profile_patterns)),
)

关于Django:如何从 urls.py.. 中的同一文件添加其他 url 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26928103/

相关文章:

python - django zip 不等列表

python - Django Rest Framework ViewSet 出现 404 错误

未授予 Django 管理员访问权限

django - 在 Django 模型中添加新字段后出现错误

python - 自定义验证时引发 ValidationError

python - Django 单元测试模拟

Debug=False 中不存在 url 的 Django 500 错误

javascript - 尝试从 Django 服务器设置值时,Java 脚本给出无效的语法错误

python - Django 网页上的两个 View

python - 如何避免用户在 Django 中注册已使用的电子邮件?