如何在 Django 中访问 html?

用户10102827

我想让一个 href 链接可以访问 Django 应用程序的 sub.html。我写在 index.html 就像

<p class="button"><a href="{% static 'templates/sub.html'%}">Next</a></p>

但是当我放置 Next 按钮时,Page not found (404) Request URL: http://127.0.0.1:8000/static/templates/sub.html发生错误。我的应用程序的结构是

-app
 -index
  -templates
   -index.html
   -sub.html
  -urls.py
  -views.py
 -manage.py

我的代码有什么问题?我重写了<a href="{% static 'sub.html'%}">但发生同样的错误。我应该如何解决这个问题?

达维特·阿巴特

静态文件用于样式表 javascript 和其他静态文档。您可以在 urls.py 中添加模板作为视图

from django.urls import include, path
from django.views.generic import TemplateView

urlpatterns = [
    path('sub/', TemplateView.as_view(template_name='sub.html'), name='sub')
]

然后在您的index.html模板中:<p class="button"><a href="{% url 'suub' %}">Next</a></p>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章