Environmental requirements
Download, install python3
Installation pipenv (python used to create a virtual environment)
pip3 install pipenv
Directly on the sample
Steps:
- Create and enter the directory
- Django installation with pip3
- Activating virtual environment
- Create a new Django project (project)
- Creating pages program (app) in the project
- Modify settings.py
- run
Create a project
$ mkdir helloworld
$ cd helloworld
# 激活虚拟环境,此时目录下多了个Pipfile [注意,我们后续的操作,都是在该虚拟环境中进行的]
$ pipenv shell
# 安装Django
$ pip3 install django
# 创建项目到当前目录
$ django-admin startproject helloworld_project ./
Now the directory structure is as follows:
helloworld
- Pipfile
- Pipfile.lock
- manage.py
- helloworld_project
- __init__.py
- settings.py
- urls.py
- wsgi.py
Running the Project
$ python manage.py runserver
Access http://127.0.0.1:8000/ can see the default page of Django
Creating app
A Django project which often contain multiple app, each app is responsible for part of the function.
Now let's create an app
# 创建一个名为 pages 的app。执行后,会发现当前目录下多了个pages目录
$ python manage.py startapp pages
Register at project level settings.py in our app:
# helloworld_project/settings.py
INSTALLED_APPS = [
'pages.apps.PagesConfig', # 新增
......
]
Write view file that displays hello world
# pages/views.py
from django.http import HttpResponse
def homePageView(request):
return HttpResponse('Hello, World!')
In order to allow the browser to find homePageView, we also need to write urls.py.
Navigation route request is such that: Project urls.py -> app of urls.py -> specific view.
So we need to modify finished two urls.py file is completed.
Write level app url, the app corresponding to the received requests to view function
New urls.py file in the pages directory and enter the following.
# pages/urls.py
from django.urls import path
from .views import homePageView
urlpatterns = [
path('', homePageView, name='home')
]
Next, preparation of the project file url-level, the program guide received request corresponding to app.
# helloworld_project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
$ python manage.py runserver
Now access http://127.0.0.1:8000/ , we can see our hello world displayed on the page.
Directory structure analysis
In order to be able to write your own, modify the project, we need to understand some of the key responsibilities of the file.
settings.py (project level)
Control project configuration, for example, registered app, set up databases.
urls.py
Management received a response rule. In this project allowed to specify what URL to receive, upon receipt of which is processed by the function.
manage.py
For performing various instructions Django. For example, create a project, create app, running services.
views.py
接收请求(request)并返回响应(response)的地方。 可以理解为是个编写业务逻辑的地方。
admin.py
Django有个内置的admin程序,该文件就是用于配置admin程序的。
apps.py
配置app自身的文件。
models.py
用于定义数据库模型,Django会自动为我们转换为数据库表。
migrations/
跟踪models.py文件的变化,以保持models.py和数据库的同步。这个目录里的内容,通常不用理会。
修改国内源
如果执行 pip3 install pipenv
或者 'pip3 install django' 慢或者失败,可以把pip修改为国内镜像源。
pip镜像修改为国内镜像
$ mkdir ~/.pip # 如果有.pip目录就略过这一条。
$ vim ~/.pip/pip.conf
粘贴以下内容[使用的是阿里云的源]
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com