The start of the Django foundation

Do one thing at a time, and do well
Do one thing at a time, do your best!

Django Introduction

Too much introductory content will not be written. I mainly want to say to the students who are entangled in django or flask and even other frameworks. The framework of learning is not important. It is like learning a language, and it is a door-to-door. Whether Django or flask is very powerful, it is enough for us to write a project.

Django’s MTV framework

Django is designed using the MVC framework, but more accurately it should be based on the MTV framework, which is a model-view-template. Briefly, the model is the database (responsible for data storage), the view is the back end (responsible for data processing), the template is the front end (responsible for data display), the model and view in Django correspond to models.py, views.py, and the template You need to create an html file in the templates directory, and the functions in views.py render the Html template in the templates to get the dynamic content of the page.
The search function of a Django page, the whole process is like this: get user input from the template —> request to the view —> view to get data from the model —–> view to process the data —– > Return to the model display.

Django installation

1
2
3
4
5
6
7
Install pip:
sudo apt-get install python-pip
or
yum install python-pip
Then install django:
pip install django

Description: It is recommended to build django using the Python virtual environment.

Django Common Commands

New project and app:

1
2
Django-admin.py startproject project_name New project
Python manage.py startapp app_name New APP

Database operation:

1
2
3
4
5
#Create changed file
python manage.py makemigrations
#Apply the generated py file to the database
python manage.py migrate
python manage.py flush

Use the built-in server:

1
2
3
python manage.py runserver
python manage.py runserver 8080
python manage.py runserver 10.0.0.1:80

Create an administrator:

1
2
python manage.py createsuperuser
python manage.py changepassword username

Data import and export:

1
2
python manage.py dumpdata appname > appname.json
python manage.py loaddata appname.json

Project environment terminal:

1
python manage.py shell

Description: The api in the models.py of the current project can be called from this shell.

Database command line:

1
python manage.py dbshell

You can execute the sql statement on the command line.

Creating a project

1
Django-admin startproject mysite (project name)

Note: The directory cannot have Chinese.
Turn on the built-in server:

1
Python manage.py runserver (ip:port) #default is 8000

Visit: http://localhost:8000

Create an app

There can be multiple applications in a project, one application is a web application.

1
python manage.py startapp webapp

Description: A webapp folder will be created in the same directory as manage.py.

setting.py

Add the newly defined app, here the webapp to setting.py:

1
2
3
4
5
6
7
8
9
10
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'webapp',
)

Description: Adding the app is for django to know that we created a new app that will load the app.

view.py

Define the view function (views.py):

1
2
3
4
from django.http import HttpResponse
def index(request):
return HttpResponse("This is a test page!")

Description: The view function (view.py) is a function of some column functions done on the server side. It receives a request request and returns a response.

urls.py

Define the url and modify urls.py:

1
2
3
4
5
6
7
8
from django.conf.urls import url
from django.contrib import admin
from webapp import views
urlpatterns = [
url(r'^$', views.index),
url(r'^admin/', admin.site.urls),
]

Description: urls.py is a file that defines django routes. This route is not a route in the network. In short, it is a url. It defines which functions in the view are executed when we request which urls.

Reference article

http://code.ziqiangxuetang.com/django/django-tutorial.html

  • This article only makes the most basic Django introduction. As for the specific usage of each layer of MVC and the configuration, security, deployment and other issues, it will be introduced one by one.

本文标题:The start of the Django foundation

文章作者:nmask

发布时间:2017年09月14日 - 19:09

最后更新:2019年08月16日 - 15:08

原始链接:https://thief.one/2017/09/14/1/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: