Django framework

Django
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.
C:\Users\user_name>pip install virtualenvwrapper-win
This will create the environment but we have to do the setup.
C:\Users\user_name>mkvirtualenv my_dj
We used our environment as my_dj but you can use your own name to create your environment.

If you have not installed Django then use this command
(my_dj) C:\Users\user_name>pip install django
(my_dj) C:\Users\user_name>cd smo_dj
Checking the version of Django
(my_dj) C:\Users\user_name\smo_dj>django-admin --version
Output 2.2.5. Same output we will get by using manage.py
(my_dj) C:\Users\user_name\smo_dj>python manage.py version
Note that we have installed Django in our virtual environment ( not in the full system ) . So it is only available to our my_dj environment.

Using help

python manage.py help
You will get a list of commands available.

Creating a project

Let us create a project my_dir, you can give your name as you want. First we will create one directory and name it as my_dir
(my_dj) C:\Users\user_name>md my_dir
We created one my_dir folder and our this folder is empty. Now let us create the Project
(my_dj) C:\Users\user_name>cd my_dir
Check your folder my_dir and you can see there are no files.
Inside the directory we will use this command to create the project.
(my_dj) C:\Users\user_name\my_dir>django-admin startproject my_dir
Now you can check your folder my_dir to see there are many files( and directory ) created by above command.

Starting web server.

Change to our project directory
(my_dj) C:\Users\user_name\my_dir\cd my_dir
Now start the server ,
(my_dj) C:\Users\user_name\my_dir\my_dir>python manage.py runserver
You can stop the server by using Ctrl + C Check system python manage.py check Check system

To start any app we will use this command
python manage.py startapp my_app
Templates for adding content, let us create a directory smo_temp and we will set this inside settings.py file like this.
'DIRS': [os.path.join(BASE_DIR,'smo_temp')],
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

# Create your views here.

from django.http import HttpResponse

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
Flow of a project and App


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer