Skip to content

Commit 68243d8

Browse files
committed
init
0 parents  commit 68243d8

8 files changed

+381
-0
lines changed

.gitignore

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.idea/
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
*.log
7+
local_settings.py
8+
db.sqlite3-journal
9+
10+
.env
11+
.venv
12+
env/
13+
venv/
14+
ENV/
15+
env.bak/
16+
venv.bak/
17+
18+
.idea/
19+
collectstatic/
20+
21+
.DS_Store

drf_vue_blog/__init__.py

Whitespace-only changes.

drf_vue_blog/asgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for drf_vue_blog project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'drf_vue_blog.settings')
15+
16+
application = get_asgi_application()

drf_vue_blog/settings.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""
2+
Django settings for drf_vue_blog project.
3+
4+
Generated by 'django-admin startproject' using Django 3.0.7.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.0/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/3.0/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = '-1(gw4h!_=o2@@-3-p9jj62y=9k#*^3(5*!i-$yhblsckxw!=1'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
41+
'rest_framework',
42+
]
43+
44+
MIDDLEWARE = [
45+
'django.middleware.security.SecurityMiddleware',
46+
'django.contrib.sessions.middleware.SessionMiddleware',
47+
'django.middleware.common.CommonMiddleware',
48+
'django.middleware.csrf.CsrfViewMiddleware',
49+
'django.contrib.auth.middleware.AuthenticationMiddleware',
50+
'django.contrib.messages.middleware.MessageMiddleware',
51+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
52+
]
53+
54+
ROOT_URLCONF = 'drf_vue_blog.urls'
55+
56+
TEMPLATES = [
57+
{
58+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
59+
'DIRS': [os.path.join(BASE_DIR, 'templates')]
60+
,
61+
'APP_DIRS': True,
62+
'OPTIONS': {
63+
'context_processors': [
64+
'django.template.context_processors.debug',
65+
'django.template.context_processors.request',
66+
'django.contrib.auth.context_processors.auth',
67+
'django.contrib.messages.context_processors.messages',
68+
],
69+
},
70+
},
71+
]
72+
73+
WSGI_APPLICATION = 'drf_vue_blog.wsgi.application'
74+
75+
76+
# Database
77+
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
78+
79+
DATABASES = {
80+
'default': {
81+
'ENGINE': 'django.db.backends.sqlite3',
82+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
83+
}
84+
}
85+
86+
87+
# Password validation
88+
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
89+
90+
AUTH_PASSWORD_VALIDATORS = [
91+
{
92+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
93+
},
94+
{
95+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
96+
},
97+
{
98+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
99+
},
100+
{
101+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
102+
},
103+
]
104+
105+
106+
# Internationalization
107+
# https://docs.djangoproject.com/en/3.0/topics/i18n/
108+
109+
LANGUAGE_CODE = 'en-us'
110+
111+
TIME_ZONE = 'UTC'
112+
113+
USE_I18N = True
114+
115+
USE_L10N = True
116+
117+
USE_TZ = True
118+
119+
120+
# Static files (CSS, JavaScript, Images)
121+
# https://docs.djangoproject.com/en/3.0/howto/static-files/
122+
123+
STATIC_URL = '/static/'

drf_vue_blog/urls.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""drf_vue_blog URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/3.0/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path
18+
19+
urlpatterns = [
20+
path('admin/', admin.site.urls),
21+
]

drf_vue_blog/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for drf_vue_blog project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'drf_vue_blog.settings')
15+
16+
application = get_wsgi_application()

manage.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'drf_vue_blog.settings')
9+
try:
10+
from django.core.management import execute_from_command_line
11+
except ImportError as exc:
12+
raise ImportError(
13+
"Couldn't import Django. Are you sure it's installed and "
14+
"available on your PYTHONPATH environment variable? Did you "
15+
"forget to activate a virtual environment?"
16+
) from exc
17+
execute_from_command_line(sys.argv)
18+
19+
20+
if __name__ == '__main__':
21+
main()

md/20-搭建开发环境.md

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
已经掌握如何搭建 Django 项目的同学,请往下翻到 **drf 开发预备** 的小节开始阅读。
2+
3+
## 教程的开发环境
4+
5+
本教程的开发环境为:
6+
7+
- **Win 10(64位)**
8+
- **Python 3.8.2**
9+
- **Django 3.0.7**
10+
11+
为了避免开发环境不同而导致的错误,建议读者使用相同的版本。
12+
13+
## 安装Python
14+
15+
python的安装为比较简单,首先找到[Python官方网站](https://www.python.org/),选择 python 3.8.2 的 windows 版本,下载并安装。
16+
17+
**安装时注意勾选添加python到环境变量中。**如果没有或者漏掉这一步,请安装完毕后自行添加。
18+
19+
安装完成后打开[命令行](https://jingyan.baidu.com/article/046a7b3e83a505f9c27fa9a2.html),输入`python -V`,系统打印出python的版本号,说明安装成功了:
20+
21+
```python
22+
C:\Users\dusai> python -V
23+
Python 3.8.2
24+
```
25+
26+
## 配置虚拟环境
27+
28+
**虚拟环境(virtualenv,或venv )**是 Python 多版本管理的利器,可以使每个项目环境与其他项目独立开来,保持环境的干净,解决包冲突问题。你可以将虚拟环境理解为一个隔绝的小系统。
29+
30+
**从 Python 3.3 版本开始就自带了虚拟环境,不需要安装,配置一下就可以用了。**
31+
32+
新建一个文件夹,教程中为`drf`。进入此文件夹:
33+
34+
```python
35+
E:\>cd drf
36+
E:\drf>
37+
```
38+
39+
输入配置 venv 的命令,其中的`venv`为虚拟环境的目录:
40+
41+
```
42+
E:\drf> python -m venv venv
43+
```
44+
45+
创建完成后,输入`venv\Scripts\activate.bat`,即可进入虚拟环境:
46+
47+
```
48+
E:\drf> venv\Scripts\activate.bat
49+
(venv) E:\drf>
50+
```
51+
52+
**盘符前有`(venv)`标识说明进入venv成功。**
53+
54+
## 安装Django
55+
56+
**在虚拟环境下**,输入命令 `pip install django==3.0.7`
57+
58+
```python
59+
(env) E:\drf> pip install django==3.0.7
60+
61+
Collecting django==3.0.7
62+
Using cached
63+
...
64+
...
65+
Successfully installed django-3.0.7
66+
67+
(env) E:\drf>
68+
```
69+
70+
系统打印出以上文字表示Django安装成功了。
71+
72+
> 由于国内复杂的网络环境, Pip 的下载可能非常缓慢甚至失败。国内用户请更换国内的镜像下载源,方法请自行了解。
73+
74+
## 创建Django项目
75+
76+
还是在**虚拟环境**下,在`drf`文件夹中创建Django项目:
77+
78+
```
79+
(env) E:\drf> django-admin startproject drf_vue_blog
80+
```
81+
82+
查看`drf`文件夹,发现多了`drf_vue_blog`文件夹,其结构应该是这样:
83+
84+
```
85+
drf_vue_blog
86+
│ db.sqlite3
87+
│ manage.py
88+
89+
└─drf_vue_blog
90+
│ settings.py
91+
│ urls.py
92+
│ wsgi.py
93+
└─ __init__.py
94+
```
95+
96+
这就是我们刚创建出来的项目了。
97+
98+
## 运行Django服务器
99+
100+
Django 自带一个轻量的 Web 开发服务器,也被叫做 runserver。
101+
102+
开发服务器是为了让你快速开发Web程序,通过它可以避开配置生产环境的服务器的繁琐环节。
103+
104+
开发服务器会自动的检测代码的改变,并且自动加载它,因此在修改代码后不需要手动去重启服务器,非常的方便。
105+
106+
要运行这个服务器,首先要进入`drf_vue_blog`文件夹,即含有`manage.py`文件的那个:
107+
108+
```
109+
(env) E:\drf> cd drf_vue_blog
110+
(env) E:\drf\drf_vue_blog>
111+
```
112+
113+
输入命令`python manage.py runserver`
114+
115+
```
116+
(env) E:\drf\drf_vue_blog> python manage.py runserver
117+
Performing system checks...
118+
...
119+
Django version 3.0.7, using settings 'drf_vue_blog.settings'
120+
Starting development server at http://127.0.0.1:8000/
121+
Quit the server with CTRL-BREAK.
122+
```
123+
124+
系统打印出这些信息,说明服务器启动成功了,打开chrome浏览器,输入http://127.0.0.1:8000/ ,网页中看到一个绿色的小火箭,恭喜你,项目已经正常运行了。
125+
126+
## 代码编辑器
127+
128+
django 运行起来后,我们还需要一款**代码编辑器**或者**集成开发环境(IDE)**来编辑python文件,以达到开发需求。
129+
130+
市面上有很多Python的代码编辑器或者集成开发环境可以选择。
131+
132+
新手我推荐 [Pycharm](https://www.jetbrains.com/pycharm/),虽然第一次用时需要熟悉软件的使用方法,但是它提供的各项强大的功能,比如代码提示、拼写提示等,可以帮助你避免很多基本的错误(比如打错字)。
133+
134+
如果你想要一个纯粹的文本编辑器,那么可以用 **Sublime Text 3**。它不是免费的,但是可以无限期试用,所以你不需要掏腰包。
135+
136+
进入[Sublime Text 3官网](https://www.sublimetext.com/3),下载对应版本的安装文件安装即可使用了。
137+
138+
## 浏览器
139+
140+
作为一个正经的 web 开发者,你的眼中应该只有[Chrome](https://www.google.com/chrome/)
141+
142+
## drf 开发预备
143+
144+
学习 drf 需要以 Django 的基础知识作为支撑,因此下面关于 Django 基础部分就不会展开讲解了。如果你感觉下面的代码理解起来非常困难,磨刀不误砍柴工,那么建议先阅读我的 Django 基础教程:
145+
146+
- [博客版](https://www.dusaiphoto.com/article/detail/2/)
147+
- [GitHub版](https://github.com/stacklens/django_blog_tutorial)
148+
149+
首先安装 drf 及其扩展:
150+
151+
```python
152+
pip install djangorestframework==3.11.0
153+
pip install markdown==3.2.2
154+
pip install django-filter==2.3.0
155+
```
156+
157+
158+
159+
## 总结
160+
161+
经过以上一番折腾,总算是把趁手的工具都准备齐了。
162+
163+
准备好迎接正式的挑战吧。

0 commit comments

Comments
 (0)