【问题标题】:resolve urls for a django web app解析 django web 应用程序的 url
【发布时间】:2015-06-05 14:12:40
【问题描述】:

我有一个基于 Django 并托管在 AWS 上的实时网站。它没有按照我希望的方式解析网址。

由于某些原因,如果我在浏览器中输入 example.com/docprofile/21,它会解析为 www.example.com 而不是 www.example/docprofile/21

虽然,以下所有正确诉诸www.example.com

  • http://www.example.com
  • example.com

我不知道为什么 suburl 会出现在主页上。

urls.py

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^index/$', views.index, name='index'),
    url(r'^404/$', views.error404, name='error404'),
    url(r'^privacy/$', views.privacy, name='privacy'),
    url(r'^terms/$', views.terms, name='terms'),
    url(r'^about/$', views.about, name='about'),

    url(r'^signup/$', views.signup_user, name="signup"),
    url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'm1/login.html'}, name="login"),
    url(r'^logout/$', views.logout_user, name="logout"),

    url(r'^doclistings/$', views.doclistings, name='doclistings'),

    url(r'^clinics/$', views.clinicList, name='clinicList'),

    url(r'^search/$', views.searchResults, name='searchResults'),

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

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

【问题讨论】:

  • 听起来更像是网络服务器配置问题...
  • 你能把整个urls.py贴出来吗?我看到的第一个问题是 url 模式以斜杠 / 结尾,因为您的浏览器 url 没有
  • 这可能是因为其他一些 url 模式匹配 example.com/docprofile/21 无论如何帖子中的 url 不会匹配这个。所以我不希望调用showDocProfiles
  • www.example.com/docprofile/21/ 是否按预期工作,还是会将您重定向到www.example.com?另外,例如example.com/privacy/ 重定向到www.example.com/privacy/?

标签: python django url


【解决方案1】:

问题是浏览的 url 与 url 模式中的正则表达式不匹配。

即在模式中

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

我们希望 url 以 / 结尾(/$ 部分会这样做),而在浏览的 url (example.com/docprofile/21) 中并非如此。

匹配模式的正确url是

example.com/docprofile/21/

然后由views.showDocProfile处理

【讨论】:

  • example.com/docprofile/21/ 也不解析为 www.example.com/docprofile/21/。
  • @JamesL。这很奇怪。让我检查一下是否还有其他问题
  • @JamesL。每次使用docprofile 时,是否都会重新路由回index
  • 不是每次如果我这样做 www.example.com/docprofile/29 它都会求助于 www.example.com/docprofile/29/ 只有当 www 不在 URL 中时它才会求助于 www.example .com
  • 您是否有某种规则可以处理 404 状态?例如,如果找不到页面或 id 无效,会发生什么?它会重定向到主页吗?
猜你喜欢
  • 2015-05-25
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 2012-03-26
  • 2016-08-28
相关资源
最近更新 更多