'model_type in Django template
I am learning Django from a book called Web Development with Django. In one of the activities there is a tag in one of the templates as below:
{% block title %}
{% if instance %}
Editing {{ model_type }} {{ instance }}
{% else %}
New {{ model_type }}
{% endif %}
{% endblock %}
What is the model_type? I have never seen this tag before. It is not in any of the view.py functions or anywhere else.
Solution 1:[1]
Django works on MVT[javapoint] design pattern, so whether instances,querysets must be passed through view, whether hardcoded or fetching from any Model.
model_typeis not any inbuilt property or anyting, they are passed through views.
Consider 1st example (Things are hardcoded):
Create a project and create an application
home, throughpython manage.py startapp home. createtemplatesfolder for html files inside app at the pathhome/templates/home/index.html, this meanstemplate namespacing[django-doc]
Note:You don't need to givetemplatesinAPP_DIRSinsettings.py, they are only given when we place all the templates from all apps, in outside folder, same forstatic.
index.html
<body>
<h2>Name : {{name}}</h2>
<h2>Technology : {{technology}}</h2>
</body>
views.py
from django.shortcuts import render
def home(request):
context = {
'name': 'reza',
'technology': 'django'
}
return render(request, 'home/index.html', context)
urls.py
from django.urls import path
urlpatterns = [
path('home/', views.home, name='home')
]
In above example, things are hardcoded but we generally use in loops.
Consider 2nd example (data is from database):
In this example, I have created a model and data will come dynamically.
models.py
from django.db import models
class StudentDetail(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(max_length=200)
total_marks = models.PositiveIntegerField()
class Meta:
ordering = ['name']
def __str__(self):
return f"Record of {self.name} was added."
Now, register it in admin.py to see in the admin interface.
admin.py
from django.contrib import admin
from .models import StudentDetail
@admin.register(StudentDetail)
class StudentDetailAdmin(admin.ModelAdmin):
list_display = ['id', 'name', 'email', 'total_marks']
Now, run python manage.py makemigrations, then run python manage.py migrate.
Now, create superuser to add some data manually, by python manage.py createsuperuser.
Then, add 3 or 4 records in database through admin interface.
Now, make your views to retrieve objects dynamically.
views.py
from django.shortcuts import render
from .models import StudentDetail
def home(request):
all_students = StudentDetail.objects.all()
context = {
'all_students': all_students
}
return render(request, 'home/index.html', context)
index.html
<body>
<table border='3px' cellpadding='1px' cellspacing='5px' style='text-align:center;'>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Total Marks</th>
</tr>
</thead>
<tbody>
{% for student in all_students %}
<tr>
<td>{{student.name}}</td>
<td>{{student.email}}</td>
<td>{{student.total_marks}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
In the above way, work is done, I'll recommend you to directly read and learn everything from django-doc. At first level starts with topic-guides, and also see how the documentation is organized.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sunderam Dubey |
