- clone this repository
- Install requirement file
- open directory where manage.py file is present
- open terminal type the following command
pip3 install -r requirement.txt python3 manage.py runserver
pip install Django
django-admin startproject myproject
Now you can see we have created a project folder using the above command. these files created in the root directory of your project.
Manage.py file plays a very significant role in whole Django development, it helps to run the project, create superuser and many more we will talk about it later
this is the subdirectory of the project folder, here I explain you the significance of each file
- The __init__.py file is a blank file, basically, it represents folder as a package
- settings.py file contains the whole configuration of the Django file
- URLs.py file is used to establish a path between views and template
- wsgi.py file is used for app deployment on server.
django-admin startapp myapp
Now you can see our app folder created, open it and check the significance of each file
This is an app folder, here I will show you the functionality of important files
- views.py file which is responsible for whole functionality, In this file we write our function and render them on a specific HTML page.
- models.py file where we can specify our database structure using the class-based approach. Here we can make tables, create relation on the basis of ORM.
- the admin.py file acts as a waiter between models and admin panel. It just makes the connection so that we can see our tables data in the admin panel
- static: it will contain our static files such as images, CSS and js
- templates: this folder contains our HTML files for our app
I hope your root directory looks as described in the above picture
In this picture, I have concatenated root directory of the project with static folder
In this step, I have initialized templates folder so that our Django can easily locate it.
Here I have mentioned static files directory, It will use a lot of time in this project
First understand what views.py fille does, basically it a python file which works on function here one can make function according to his own desire. Every function returns its response to the front end (HTML pages) with dynamic data.For example, let's say we have a views.py file and have a function name demo which returns its data into an HTML page name demo.html (all HTML files should in a static folder).
views.py
def demo(request): username = "rajat" context = { 'name': username } return render(request,'demo.html',context)
I hope you understood the functionality of views.py file, now open the HTML file and call data using Jinja2 (The Python Template Engine)
jinja 2 template call variable data in {{}} format, it also use flow control as well as conditional statements using {}.
Now open demo.html file
{% load staticfiles %}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>User name is : {{name}}</h1>
</body>
</html>
Now our function, HTML page are ready now we need to create a separate URL for this HTML page so we need to edit urls.py of our project.
urls.py
from django.contrib import admin from django.urls import path from myapp import views urlpatterns = [ path('admin/', admin.site.urls), path('', views.demo, name = 'demo'), ]
Here I just configured its URL path I put empty string because I want it landing page. Suppose one just one to name it as Path('demo', views.demo) then one should open this page like this http://127.0.0.1:8000/demo.
enter this command on your project root directorypython3 manage.py createsuperuser
this command created your superuser now one can run the local server and view the admin panel
python3 manage.py runserver
Thank you for reading this article