【问题标题】:Django views rendering just one page and wont render the restDjango 视图只呈现一页,不会呈现其余页面
【发布时间】:2019-05-02 15:19:57
【问题描述】:

所以我在 macosx 上使用 django 1.8,我在配置 html 时遇到问题,即当我尝试加载另一个页面时,除了设置为默认页面的页面(索引是默认页面),它只是刷新我拥有的默认页面在 urls.py 中设置,我无法访问除该页面之外的任何其他页面,但在 url 栏中我可以看到我正在访问正确的 html 文件,因为它这么说但页面没有改变....这是我的代码:

app/urls.py-----------

urlpatterns = [
    url(r'^contact/', views.contact, name='contact'),
    url(r'^projects/', views.projects, name='projects'),
    url(r'^services/', views.services, name='services'),
    url(r'^', views.index, name='index'),
    url(r'^about/', views.about, name='about'),

这些都是我试图安装的页面 主要 urls.py---------- 从应用导入视图

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^',include('app.urls')),

]

这是我的观点。py-----------

def contact(request):
    return render(request, 'app/template/contact.html',{})

def about(request):
    return render(request, 'app/template/about.html',{})

def projects(request):
    return render(request, 'app/template/projects.html',{})

def services(request):
    return render(request, 'app/template/services.html',{})

def index(request):
    return render(request, "app/template/index.html",{})

【问题讨论】:

  • 不清楚发生了什么。如果你在浏览器上输入,你能访问任何其他网址吗?
  • 您描述的行为只会发生在此代码中的“about”中,您可以正常访问“contact”、“projects”和“services”。
  • 是的,但只有我设置为默认值的索引或我设置为默认值的任何其他索引我可以访问,但是当我尝试访问 about.html 时,url 会更改,但我从 index.html 获取代码@瓦卢卡斯
  • @MilanRasovic,如果您访问像/contact/ 这样的网址会发生什么?
  • @Sean Francis N. Ballais 什么都一样,我可以发送 scrrenshot url 更改,但我得到相同的 index.html 页面

标签: python html django url view


【解决方案1】:

https://docs.djangoproject.com/en/1.8/intro/tutorial03/

你需要 $ 来结束字符串。在您的情况下,他链接所有以所有开头的内容。

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

你的意见.py:

def contact(request):
    return render(request, 'app/contact.html',{})

def about(request):
    return render(request, 'app/about.html',{})

def projects(request):
    return render(request, 'app/projects.html',{})

def services(request):
    return render(request, 'app/services.html',{})

def index(request):
    return render(request, "app/index.html",{})

【讨论】:

  • 抓得好,伙计!
  • 使用 instabook.urls(main urls.py) 中定义的 URLconf,Django 尝试了这些 URL 模式,顺序如下: ^admin/ ^contact/ [name='contact'] ^projects/ [ name='projects'] ^services/ [name='services'] ^$ [name='index'] ^about/ [name='about'] ^assets\/(?P.*)$当前路径 about.html 与其中任何一个都不匹配。 @尼科齐默
  • 我在输入解决方案时收到该错误@SeanFrancisN.Ballais
  • 在哪里? @NicoZimmer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2013-03-22
  • 1970-01-01
  • 2012-04-22
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多