简体   繁体   中英

How can I access html in Django?

I want to make a href link can access sub.html of Django app. I wrote in index.html like

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

but when I put Next button,Page not found (404) Request URL: http://127.0.0.1:8000/static/templates/sub.html error happens. My app's structure is

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

What is wrong in my codes?I rewrote <a href="{% static 'sub.html'%}"> but same error happens.How should I fix this?

Static files are for stylesheets javascript and other static documents. you can add a template as view in your 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')
]

Then in your index.html template: <p class="button"><a href="{% url 'suub' %}">Next</a></p>

You must refer to urls and then urls refer to views and after that view render html tamplate

If you using name attribute in url like path('url',views.yourview,name='yourname') then you can use:

{%  url 'your name'   %}

in your template

Example:

<a href='{% url 'yourname'  %}'>Link</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM