Python study notes --Day09
Django simple entry
Introduction to Django
Django is an open source Web application framework written in Python. MTC mode using a frame, i.e. the model M, and template view V T. It was originally to be open for Lawrence Publishing Group's management to some content-based news website, that is CMS (content management system software). It was released in July 2005 under the BSD license.
installation
By pip installation, execution
pip install Django
You can also specify the version of Django installation
pip install django==2.1.5
View django version of the following command
python -m django --version
Or another way
django-admin --version
In fact, there is a third way
C:\>python
>>> import django
>>> django.get_version()
The amount, there is a way, but this is dependent libraries and View installed version by pip
pip freeze
pip list
Project Creation
Creating a project
django-admin startproject 项目名
You will get a django project, the project directory as follows
- manage.py
- test_django
- __init__.py
- settings.py
- urls.py
- wsgi.py
__init__.py
It is an empty file that explains test_django is a Python package, settings.py
Django project's overall profile, urls.py
url profile, like a "directory", wsgi.py
Python server gateway interface, manage.py
Django project file management, and project interact command line toolset inlet.
In Django, each module uses a django application to develop. A project is composed by a number of applications, each application will complete their specific function. Create an application :
python manage.py startapp 应用名
Note : To enter the project directory and then create the application.
The following application directory
- migrations
- __init__.py
- __init__.py
- admin.py
- apps.py
- models.py
- tests.py
- views.py
After the re-configuration is required for registration. Modify settings.py
the INSTALLED_APPS configuration items.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'a_test', # 注册刚刚建立的应用
]
The project started as a web server command
python manage.py runserver [ip:port]
The above ip and port are optional, you can specify which runs ip address and port.
After that, the browser access localhost:8000
can see our Django default welcome page.
The install worked successfully! Congratulations!
However, to be reminded that this server just put Django's own server for development and testing, not production-level server, the server will not be used anywhere in a production environment and relevant. And when you modify the code to make the code to take effect, it does not require frequent re-start the server because it will need in the case of each of the access requests overloads Python code. But sometimes still need to manually restart the server, such as adding new files.
Command prompt Ctrl + C
shut down the server.
Modify settings.py
the time zone and language
# 设置语言代码
LANGUAGE_CODE = 'zh-hans'
# 设置时区
TIME_ZONE = 'Asia/Shanghai'
And then refresh the page and found no change?
Epilogue
Today write this now, sort of little, a little tired, and then continue to operate Django back tomorrow.
If you find my articles where there is an error or have any good ideas can contact me, we study together progress together, my email address is [email protected]
let’s do more of those!