2019.5.28 computer suddenly crashes. . Fortunately, auto-save, this great feature
Learning content: Django Getting Started: environment configuration, basic instructions, you create a project, create app, catalog description, Django view, Django route
############################################################################################
Comparison of the three framework: Django (the largest use), Flask, tornado
Description: open source, MCV, the development of high efficiency, powerful, attention to safety
Course content: Course preparation (environment), Django project first experience, first met Django model layer, view the module acquaintance Django
1. Course preparation (environment)
python 3.5+, python scientific computing environment Anaconda (many third-party libraries)
Django 2.0 "pip install django == 2.0" enter "django-admin" confirm installing
2.Django project early experience:
# Acquaintance Django project:
i. Django command (Basic Operation)
startproject: # Create a Django project
startapp: # create a Django application
check: # check the integrity of the project
runserver: # Easy local operation Django project
shell: # into the Python Shell environment Django project
test: # execute test cases Django
ii. Django command (related database)
makemigrations # Create a model change migration file
migrate: a command to create a migration file on execution #
dumpdata: # database to export data to a file
loaddata: # The import file data into the database
. Iii create a project:
①:django-admin startproject django_introduction : django_introduction(项目名)
cd django_introduction> dir: enter the project folder to see what's inside
Initialize (create) the project directory:
django_introduction> mangae.py: project management
django_introduction>django_introduction:
__init__.py
settings.py profile
urls.py routing file
wsgi.py contains the required content wsgi
②: Python manage.py the runserver : Run the Django project initialization, the default address https://127.0.0.1:8000 welcome screen appears Django represent the initial configuration is complete.
# Acquaintance Django application:
. I Django application VS Django project:
a. a Django project is a Django-based Web application
b. a Django application is a reusable Python packages. Applications can manage their own models, views, templates, and static routing files
A Django project contains a set of configuration and number of Django application. Django application can be used for different projects (reusable python package).
. Ii Django application catalog introduction:
manage.py startapp Python blog : create a project called blog application
Initialize (create) the project directory:
django_introduction>blog :__init__.py
admin.py Admin module management objects defined place
apps.py declared local applications
models.py definition of local application of the model
Local applications written test cases tests.py
Views.py view local processing
urls.py (self-created) local routing management applications
django_introduction>blog>migrations :
#Django hello world:
. I Django view:
There is no time frame: hello.html impossible to express all of the content on the page using HTML
Django view generated content : view.py enter:
from django.http import HttpResponse
def hello_world(request):
return HttpResponse("Hello World")
. Ii Django route:
Django runserver can see the welcome page but the request can not be reached just view function
This requires configure the routing function and binding view the URL of , enter> blog> urls.py in:
from django.urls import path, include
import blog.views
urlpatterns = [
path("hello_world", blog.views.hello_world)
]
Then> django.introduction> urls.py in this route has been found in the admin:
import ...
urlpatterns = [
path('admin/', admin.site urls),
path ( 'blog /', the include ( 'blog.urls')) # If the address contains blog / put forward his file to blog.urls route inside
]
Go> django_introduction> settings.py find INSTALLED_APPS = [] in the last added:
#myapp
'blog.apps.BlogConfig',
Found in> blog> apps.py in class BlogConfig (AppConfig) , there has been a " name = 'Blog' "
. iii above are configured after, the terminal input: Python manage.py the runserver; browser, enter "https://127.0.0.1:8000/blog/hello_world" see the page in the output hello world string.
review: