In this Python Django Tutorial, we will learn where to save the base template for all apps in Django. And, these are the following topics that we are going to discuss in this tutorial.
- Python Django project setup
- Python Django where to save base template for all apps
Python Django project setup
In this section, we’ll learn to set up a Django project.
CREATE PROJECT: First, we create a Django Project. For this, type the below-mentioned command in the terminal.
django-admin startproject BookStore
- Here, we create a Django project called BookStore.
CREATE APP: Following that, as part of the project, we’ll build an App. Run the following command in the terminal to create it.
python manage.py startapp Books
INSTALL APP: The above-created Books App must be included in the settings.py file.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Books'
]
CREATE TEMPLATES: Django templates keep designing of Django applications. The template folder contains all of the templates you’ll need in your various Django Apps.
In most cases, the templates folder is created and retained in the sample directory, which also contains manage.py.
Alternatively, you can create a distinct template folder for each app.
ADD TEMPLATES: To configure the Django template system go to the settings.py file and change the DIRS to the path of the templates folder.
- When you create a single template folder:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['Templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
- When you create a distinct template folder:
You don’t need to update the DIRS in settings.py if you’re maintaining the template for each app separately. Make sure your app is included in settings.py’s INSTALLED APPS.
PROJECT URLs: Add the following code in the BookStore urls.py file.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('Books.urls')),
]
APP URLs: Add the following code in the Books urls.py file.
from django.urls import path
from . import views
urlpatterns = [
path('', views.BookForm, name='BookForm'),
]
CREATE FORM: Now, we create a form. And the below code snippets to the forms.py file of the Books app.
from django import forms
class BookRecord(forms.Form):
Book_Name = forms.CharField(label='Book Name')
Author_Name = forms.CharField(label='Author Name')
- Here, we create a BookRecord form class with two CharField.
CREATE VIEWS: Add the below code to the views.py file.
from django.shortcuts import render
from Books.forms import BookRecord
def BookForm(request):
if request.method == "POST":
form = BookRecord(request.POST)
if form.is_valid():
BookName = form.cleaned_data['Book_Name']
AuthorName = form.cleaned_data['Author_Name']
context ={
'BookName':BookName,
'AuthorName':AuthorName
}
return render(request, 'index.html',context)
else:
form = BookRecord()
return render(request,'home.html',{'form':form})
Read: How to encrypt and decrypt password in Django
Python Django where to save base template for all apps
In this section, we’ll learn where to save the base template for all apps.
CREATE BASE TEMPLATE: Now, create an HTML file base.html and add the following code.
<!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">
<title>PythonGuides</title>
</head>
<body>
<center>Welcome to PythonGuides Book Store</center>
{% block body %}
{% endblock %}
</body>
</html>
- The block tag is used to describe a block that can be overridden by child templates.
- In other words, specifying a block in the base template means that the content for this region will originate from a child template file.
- Take a look at this base.html template, which has one block for the body.
CREATE HOME TEMPLATE: Now, create an HTML file home.html and add the following code.
{% extends 'base.html' %}
{% block body %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<form method="POST" class="post-form" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Submit</button>
</form>
{% endblock %}
</body>
</html>
- After you’ve created your base template, you can add child templates to it.
- The extends keyword can be used to execute this.
- So we can place that tag at the very top of the index.html file.
CREATE INDEX TEMPLATE: Now, create an HTML file index.html and add the following code.
{% extends 'base.html' %}
{% block body %}
<!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">
<title>Document</title>
</head>
<body>
<p>
The book <b>{{BookName}}</b> written by <b>{{AuthorName}}</b> successfully recorded.
</p>
</body>
{% endblock %}
</html>
- In the index.html file, we are just using the variable to get the Book Name value and Author Name value.
Run Server: Start the server and access the form by defining the URL as http://127.0.0.1:8000.
You may also like to read the following Django tutorials.
- Python Django concatenate string
- Python Django MySQL CRUD
- Python Django app upload files
- Python Django random number
- Python Change Django Version
- Python Django round to two decimal places
- Python list append Django
In this Python Django Tutorial, we have discussed Django where to save base template for all apps and we have also discussed the following topics in this tutorial.
- Python Django project setup
- Python Django where to save base template for all apps
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.