Search code examples
pythondjangosettingsvs-web-site-project

Link correctly to index.html in Python Django


I have a Python Django project with the following files.

proj1/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('website.urls')),
]

proj1/website/views.py

from django.shortcuts import render

def index(request):
    return  render(request, 'index.html', {})

proj1/website/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name="index"),
]

Under proj1/website/templates I have a few .html files. How do I link to those in my .html files? I have currently the following written in index.html

<a href="index.html">Home</a>

I get error message when I press the link for the index.html though.

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/index.html
Using the URLconf defined in leam.urls, Django tried these URL patterns, in this order:

admin/
[name='index']
The current path, index.html, didn't match any of these.

What am I doing wrong? Do I need to specify where the index.html is located with a full path, or what is wrong here? The index.html loads with run manage.py.


Solution

  • <a href="{% url 'website:index' %}">Home</a>
    
    from django.urls import path
    from . import views
    
    app_name = 'website'
    
    urlpatterns = [
        path('', views.index, name="index"),
    ]
    

    Try this, it should work.
    The formula is {% url '<namespace or app name>:<view name>' %}