【问题标题】:having problem loading url from the views and url in django在 django 中从视图和 url 加载 url 时出现问题
【发布时间】:2021-11-25 11:11:37
【问题描述】:

我正在使用 GitHub 中免费提供的自定义引导模板构建网页。我试图将 Django 连接到后端。我在使用 url 和视图时遇到问题。我已经完美地提到了,但我找不到页面。

urls.py 文件

from django.urls import path, include
from django.conf.urls.static import static
from . import views
urlpatterns = [
    path('', views.homepage, name='homepage'),
    path('publications', views.publications, name='publications'),
    path('alumni', views.alumni, name='alumni'),
    path('contact', views.contact, name='contact'),
    path('joinus', views.joinus, name='joinus'),
    path('phdstudents', views.phdstudents, name='phdstudents'),
    path('pi', views.pi, name='pi'),
    path('project1', views.project1, name='project1'),
    path('research', views.research, name='research'),
    path('researchers',views.researchers, name='researchers'),
    path('shiladit', views.shiladit, name='shiladit'),
    path('students', views.students, name='students'),
    path('team', views.students, name='team'),
]

views.py

from django.shortcuts import render
def homepage(request):
    return render(request, 'lab/index.html')
def publications(request):
    return render(request, 'lab/publications.html')
def alumni(request):
    return render(request, 'lab/alumni.html')
def contact(request):
    return render(request, 'lab/contact.html')
def joinus(request):
    return render(request, 'joinus.html')
def phdstudents(request):
    return render(request, 'phd_students.html')
def pi(request):
    return render(request, 'lab/pi.html')
def project1(request):
    return render(request, 'lab/project1.html')
def publications(request):
    return render(request, 'lab/publications.html')
def research(request):
    return render(request, 'lab/research.html')
def researchers(request):
    return render(request, 'researchers.html')

当我单击导航栏重定向页面时,我收到错误消息。但是当我给出网址时,它正在加载如何纠正这个问题。

【问题讨论】:

  • 你想要的 URL 是http://127.0.01:8000/research(没有/index.html),这就是你的urls.py 中的内容。导航栏模板如何生成链接?
  • 你检查过http://127.0.01:8000/lab/research.html 吗?
  • 感谢您指出@IainShelvington 我没有在导航栏模板中删除.html

标签: python django django-views django-urls


【解决方案1】:

*在模板中(Html):*
  • 移除

<ul>
  <li><a href="research.html">Research</a></li>
</ul>
  • 试试这个:

    <ul>
      <li><a href="/research/">Research</a></li>
    </ul>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    相关资源
    最近更新 更多