【问题标题】:How to set root url in django urls.py ? Django 1.8如何在 django urls.py 中设置根 url?姜戈 1.8
【发布时间】:2015-07-15 14:07:45
【问题描述】:

我现在刚开始使用 django,正在配置 url。我能够映射不同的 url,如 /posts、/posts/create 等。不知何故,我无法配置根 url,我不确定我在做什么错。这是我的配置:

urls.py

url(r'^$',homeViews.posts),
# url(r'^blog/', include('blog.urls')),
url(r'^posts/',homeViews.posts),
url(r'^createPost/',homeViews.createPost),

这是查看代码

def posts(request):
  t = get_template('index.html')
  postList = Post.objects()
  html = t.render(Context({'posts':postList}))
  return HttpResponse(html)

奇怪的是,当我在设置中设置 DEBUG=True 时,root url '^$' 有效,但 DEBUG=False(400 错误请求)无效。不知道出了什么问题。 请帮帮我。

编辑:

这里是完整的 urls.py

from django.conf.urls import url
from rrdquest import views as homeViews
from manage.user import views as userViews


urlpatterns = [
# Examples:
url(r'',homeViews.posts),
# url(r'^blog/', include('blog.urls')),
url(r'^posts/',homeViews.posts),
url(r'^createPost/',homeViews.createPost),
url(r'^createUser/',userViews.createUser),
url(r'^post/(?P<title>[a-z,A-Z]+)/$',homeViews.post),
]

干杯!

【问题讨论】:

  • 请发布完整的urls.py
  • 您的控制台出现什么错误?
  • 我已经用完整的 url 配置更新了我的问题。当 DEBUG=False 时,控制台给出 [05/May/2015 17:49:42]"GET / HTTP/1.1" 400 26
  • settings.py 中的 ALLOWED_HOSTS 怎么样?
  • 你明白了,阿德姆! ALLOWED_HOSTS 有 127.0.0.1 并使用 127.0.0.1:8000 运行它,尽管 css 不适用但视图有效。非常感谢。如何关闭问题以接受您在 cmets 中的回答。

标签: python django web


【解决方案1】:

ALLOWED_HOSTS 是设置的必需部分,

所以应该这样设置ALLOWED_HOSTS,在settings.py

ALLOWED_HOSTS = ['*']

或者像这样

ALLOWED_HOSTS = ['localhost', '127.0.0.1']

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 2019-01-13
    • 2020-08-17
    • 1970-01-01
    • 2021-11-08
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多