I am a beginner in HTML templates and Django.
basic_app_base.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Base</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<nav >
<ul >
<li><a href="{% url 'basic_app:list' %}">School</a></li>
<li><a href="{% url 'admin:index' %}"></a></li>
<li><a href="#"></a></li>
</ul>
</nav>
<div >
{% block body_block %}
{% endblock %}
</div>
</body>
</html>
school_list.html
{% extends "basic_app/basic_app_base.html" %}
<!-- {% load static %} -->
{% block body_block %}
<h1>Here are the list of all the schools!</h1>
<ol>
{% for school in schools %}
<h2><li><a href="{{school.id}}">{{school.name}}</a></li></h2>
{% endfor % }
</ol>
{% endblock %}
**Error:**TemplateSyntaxError at /basic_app/
Invalid block tag on line 10: 'endblock', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
Views.py
from django.shortcuts import render
from django.views.generic import (View,TemplateView,
ListView,DetailView)
from . import models
# from django.http import HttpResponse
# Template views with CBV
class IndexView(TemplateView):
template_name='index.html'
# List View
class SchoolListView(ListView):
context_object_name='schools'
model=models.School
template_name='basic_app/school_list.html'
# Detail View
class SchoolDetailView(DetailView):
context_object_name='school_detail'
model=models.School
template_name='basic_app/school_detail.html'
models.py
from django.db import models
from django.urls import reverse
# Create your models here.
class School(models.Model):
name=models.CharField(max_length=256)
principal=models.CharField(max_length=256)
location=models.CharField(max_length=256)
def __str__(self):
return self.name
class Student(models.Model):
name=models.CharField(max_length=256)
age=models.PositiveIntegerField()
school=models.ForeignKey(School,related_name='students',on_delete=models.CASCADE)
def __str__(self):
return self.name
urls.py
from django.urls import include, re_path
# from django.conf.urls import url
from basic_app import views
app_name='basic_app'
urlpatterns = [
re_path(r'^$',views.SchoolListView.as_view(),name='list'),
re_path(r'^(?P<pk>\d )/$',views.SchoolListView.as_view(),name='detail')
]
I need output like the following image, when clicking on school page :
CodePudding user response:
I found the error, and I was giving too much space after the % when closing the endfor tag.
error on school_list.html file in line 8 :{% endfor % } too much space after the % when closing the endfor tag.
solution: {% endfor %}
"After correcting this error on the line 8, it worked for me."
CodePudding user response:
Need remove extra space afrer % in {% endfor % }
after correction
{% extends "basic_app/basic_app_base.html" %}
<!-- {% load static %} -->
{% block body_block %}
<h1>Here are the list of all the schools!</h1>
<ol>
{% for school in schools %}
<h2><li><a href="{{school.id}}">{{school.name}}</a></li></h2>
{% endfor %} <---------------------- in this line need correction, put {% endfor %} insted of {% endfor % }
</ol>
{% endblock %}