程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django basic tutorial

編輯:Python
  • Personal website :【 Hai Yong 】【 Fishing games 】【 Develop document navigation
  • 🤟 Interesting and humorous artificial intelligence learning website : Artificial intelligence
  • Free and practical computer related knowledge question bank : Come in and hang out

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 .

Why choose Django frame ?

  • Excellent documentation and high scalability .
  • Used by top multinationals and companies , for example Instagram、Disqus、Spotify、Youtube、Bitbucket、Dropbox etc. , And the list never ends .
  • The easiest framework to learn , Rapid development and full inclusion of batteries .
  • Study Django The last but not the least important reason is Python,Python It has huge libraries and functions , for example Web Scrapping、 machine learning 、 The image processing 、 Scientific calculation, etc . You can combine all this with Web Application integration and do many, many progressive things .

Django framework

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 .

Django Installation

  • If not installed in your system python3( Depending on your system and operating system configuration ), Please from here install . Try downloading the latest version of python, This time it is python3.6.4.
  • Be careful stay Linux and Mac Install in Django Is similar to that of , Here I am Windows Show it in , Just open the terminal instead of the command prompt and execute the following command .
  • install pip

Open a command prompt and enter the following command

python -m pip install -U pip
  • Installing a virtual environment

stay cmd Enter the following command

pip install django
  • Set up the virtual environment - Setting up a virtual environment will allow you to edit dependencies that are not normally allowed on your system . Follow these steps to set up the virtual environment -
  1. By means of cmd- To create a virtual environment
virtualenv env_site
  1. Use this command to change the directory to env_site -
cd env_site
  1. go to env_site In the script directory and activate the virtual environment -
cd Scripts
activate
  • install Django

Install... By giving the following command django

pip install django

Create project

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved