Home > Back-end >  Django 3x built in url template tag // dynamic urls via name pattern
Django 3x built in url template tag // dynamic urls via name pattern

Time:09-16

Hello fellow human beings, I try to build a rather simple application and I have a struggle with handling url template tags. I looked up many different sources and coded the project 3 times new I have no idea whats the problem any suggestion is a big help. thanks

C:.
├───nps
│   ├───templates
│   └───__pycache__
└───user
    ├───migrations
    │   └───__pycache__
    ├───templates
    │   └───user
    └───__pycache__

nps/urls.py

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

#include urls from user-app
urlpatterns = [
    path('',TemplateView.as_view(template_name='homepage.html')),
    path('user/',include('user.urls')),
    path('admin/', admin.site.urls),
]

user/urls.py

from django.urls import path
from . import views as user_views

#define names for the specific urls
urlpatterns = [
   path('home/',user_views.home,name='user-home'),
   path('login/',user_views.loginPage,name='user-login'),
   path('register/',user_views.registerPage,name='user-register')
]

#---------------------------------------------------------------------------------------#

base layout for templates

user/templates/user/base.html

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
   <title>Document</title>
</head>
<body>
   {%block main%}{%endblock main%}
</body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
</html>

home.html with navlinks and url tags

user/templates/user/home.html

{%extends 'user/base.html'%}
{%block main%}
<ul class="nav justify-content-center">
   <li class="nav-item">
     <a class="nav-link active" aria-current="page" href="#">Active</a>
   </li>
   <li class="nav-item">
     <a class="nav-link" href="{%url'user-home'%}">Home</a>
   </li>
   <li class="nav-item">
     <a class="nav-link" href="{%url'user-login'%}">Login</a>
   </li>
   <li class="nav-item">
     <a class="nav-link" href="{%url'user-register'%}">Register</a>
   </li>
 </ul>
{%endblock main%}

#----------------------------------------------------------------------------------------# ERROR MESSAGE

TemplateSyntaxError at/user/home

Error during template rendering

CodePudding user response:

You need to add a space between url and the name of the URL, so `{% url 'user-home' %} instead of {% url'user-home' %}:

<ul class="nav justify-content-center">
   <li class="nav-item">
     <a class="nav-link active" aria-current="page" href="#">Active</a>
   </li>
   <li class="nav-item">
     <a class="nav-link" href="{% url 'user-home' %}">Home</a>
   </li>
   <li class="nav-item">
     <a class="nav-link" href="{% url 'user-login' %}">Login</a>
   </li>
   <li class="nav-item">
     <a class="nav-link" href="{% url 'user-register' %}">Register</a>
   </li>
 </ul>
  • Related