Django url tags linking

Viewed 66

i'm new into django. I created a project named marketbook, it has a main templates directory and an app called users. Inside my app (users) i have templates folder as well with a file called login.html (templates/users/login.html. I want to link this login.html in my app to a link in my file (navbar.html) in my main templates director folder. what should be the url tag to use?

 <a href="{% url 'users:login' %}" class="btn" style="color: white; background-color: #fd5e14; margin-left: 10px; "type="submit" id="header-links">Log In</a>
1 Answers

In the settings.py file, the TEMPLATES variable is set to the field: 'DIRS': ['templates']. This is so that you can find the main home.html template, which is located in the templates folder, which is located in the folder where the application folder is, the manage.py module.

start the server: python manage.py runserve

go to: http://localhost:8000/users/home/

press the button and go to: http://localhost:8000/users/navbar/

urls.py (urls application)

from django.urls import path
from .views import home, navbar

urlpatterns = [
    path('home/', home),
    path('navbar/', navbar, name='navbar'),
]

views

from django.shortcuts import render

def home(request):
    return render(request, "home.html")

def navbar(request):
    return render(request, "users/navbar.html")

home.html

 <a href="{% url 'navbar' %}" class="btn" style="color: white; background-color: #fd5e14; margin-left: 10px; "type="submit" id="header-links">Log In</a>

navbar.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>
   <h2>This is a navbar template</h2>
</body>
</html>
Related