To use Django framework for our projects it is better to create one virtual environment wrapper , here is the command to create virtual environment for our Django project.
This will create one folder my_dj inside your smo_dj3 directory with sub - directories and files.
To activate the virtual environment. ( these files are created in above command )
We will create one home.html inside our smo_temp directory and let us keep this simple code inside it.
Welcome to plus2net.com
views.py
Inside my my_dir/views.py we can keep this code
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return render(request,'home.html')
The above code will display the text written inside home.html when we open URL http://127.0.0.1:8000/my_dir/
Publishing dynamic content
We can use our home.html file to publish dynamic content. Change the code inside home.html to this.
Welcome to {{site}} to learn Python,<br>
this is inside smo_temp, home.html file
Now let us change the view.py file like this.
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return render(request,'home.html',{'site':'plus2net'})
django template language ( DTL )
We will create one template file temp_basic.html like this
<html>
<head>
<title>This is basic template file </title>
</head>
<body>
{% block content %}
{% endblock %}
<br><br>
<center>Displayed by using template file temp_basic.html</center>
</body>
</html>
This is the template we will use to display our dynamic content we created obove. Let us change the home.html file like this.
{% extends 'temp_basic.html' %}
{% block content %}
Welcome to {{site}} to learn Python,<br>
this is inside smo_temp, home.html file
{% endblock %}
Now when we open our URL http://127.0.0.1:8000/my_dir/ we will get the content through our template page. Here is the output
Welcome to plus2net to learn Python,
this is inside smo_temp, home.html file
Displayed by using template file temp_basic.html
( One more Example )Creating Project
Navigate to the directory where we want to store our Project, let us give the name of the project as my_proj. I want to keep this project inside my D:\testing\ folder. My path should be like this. Note the virtual environment my_dj.
my_proj The root or outer directory of the project. manage.py Command line utility to interact with Django Project
my_proj/__init__.py : An empty file my_proj/settings.py : configuration of the project my_proj/urls.py : Table of contents of sites my_proj/asgi.py : for ASGI compatible web servers mysite/wsgi.py : for WSGI compatible web servers
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 06, 2022 - 15:20:35
Django version 4.0.1, using settings 'my_proj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Creating my_app application
To create one application ( my_app ), we will move to directory where our manage.py file is available.