Django It's based on Python Of Web frame , Allows you to quickly create efficient Web Applications . It is also known as the frame containing the battery , because Django Provide built-in functionality for all content , Include Django Management interface 、 Default database - SQLlite3 etc. . When you build a web site , You always need a set of similar components : A way to handle user authentication ( register 、 Sign in 、 sign out )、 Website management panel 、 Forms 、 How to upload files, etc .Django Provides you with ready-made components for you to use , It can also be used for rapid development .
Django be based on MVT( Model - View - Templates ) framework .MVT Is a method for developing Web Software design patterns for applications .
MVT The structure has the following three parts ——
Model : The model will act as an interface to the data . It's responsible for maintaining data . It's the logical data structure behind the entire application , To database ( It's usually MySql、Postgres Relational database ) As a representative .
View : The view is the user interface —— What you see in your browser when you render a website . It consists of HTML/CSS/Javascript and Jinja Document representation .
Templates : The template is made by HTML The static part of the output and some special syntax components that describe how to insert dynamic content .
Open a command prompt and enter the following command
python -m pip install -U pip
stay cmd Enter the following command
pip install django
virtualenv env_site
cd env_site
cd Scripts
activate
Install... By giving the following command django
pip install django
Let's examine how to put Django Install to your PC Then use it to create a basic project .
In your PC Start the Django project , Please open the terminal and enter the following command
django-admin startproject Project name
A name will be created projectName New folder for . Use the terminal input command to enter the project
cd Project name
Now run ,
Python manage.py runserver
Now access http://localhost:8000/
Create application Django Known for its unique and fully managed application architecture . For each function , You can create an application as if it were a completely separate module . This article will take you through how to create a basic application and use it to add functionality .
In your Django Create a basic application in the project , You need to go to include manage.py And enter the directory of commands from there :
python manage.py startapp projectApp
Now you can see your directory structure as follows :
Consider the applications in the project , You need to INSTALLED_APPS Specify the project name in the list , As shown below settings.py in :
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'projectApp'
]
therefore , We finally created an application , But to use url Rendering applications , We need to include the application in our main project , So that you can render the url. Let's explore . Move to projectName-> projectName -> urls.py And add the following code to the title
from django.urls import include
Now? , stay URL In the pattern list , You need to specify an app name to include your app url. This is the code for it -
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
# Enter the app name in following syntax for this to work
path('', include("projectApp.urls")),
]
Now you can use the default MVT Models are created in your application URL、 Model 、 View etc. , They will be automatically included in your main project .
Django Apps Independence is the main feature of , Each application supports the main project as a separate unit .