文章

1. 第一个Django项目

前期准备

Django在线文档

下载pipenv

pip install pipenv

下载: vscode

VS Code必备插件:

  • python

  • pylint

    在vscode中会出现某些生成代码存在格式问题, 如import未在文件头等, 可以通过注释解决, 如:

    pylint: disable=C0415

  • autopep8

创建项目

mkdir project
cd project 

pipenv install django
pipenv shell

django-admin startproject projectname .

创建 Django app

1
2
python manage.py startapp app
python manage.py runserver

settings.py文件中添加创建的app

1
2
3
4
5
6
7
8
9
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    # 'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'playground'
]

编写一个View

在创建的app中找到views.py文件

1
2
3
4
5
6
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def say_hello(request):
    return HttpResponse('hello world')

在app目录下创建urls.py文件

1
2
3
4
5
6
7
# 这是app内部的urls配置
from django.urls import path
from . import views

urlpatterns = [
    path('hello/', views.say_hello)
]

在项目的urls.py文件中配置子程序的url

1
2
3
4
5
6
7
8
9
10
from django.contrib import admin
# 需要包含其他的配置文件,添加 `include` 引用
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    # 配置子app的路由
    path('playground/', include('playground.urls'))
]

使用模板

在app目录中创建一个模板目录 templates

创建一个html文件, 比如hello.html, 编辑代码

1
<h1>hello </h1>

修改app/views.py的代码

1
2
3
4
5
6
7
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def say_hello(request):
    return render(request, 'hello.html' , {'name': 'Nestor'})

使用Django debug toolbar进行调试

文档:https://django-debug-toolbar.readthedocs.io/en/latest/

1
pipenv install django-debug-toolbar
  1. settings.py文件中添加调试工具相关配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    INSTALLED_APPS = [
        # ...
        "debug_toolbar",
        # ...
    ]
       
    MIDDLEWARE = [
        # ...
        "debug_toolbar.middleware.DebugToolbarMiddleware",
        # ...
    ]
       
    # 直接添加在`settings.py`文件中 
    INTERNAL_IPS = [
        # ...
        "127.0.0.1",
        # ...
    ]
    
  2. urls.py配置文件中添加debug路由

    1
    2
    3
    4
    5
    6
    
    from django.urls import include, path
       
    urlpatterns = [
        # ...
        path("__debug__/", include("debug_toolbar.urls")),
    ]
    
本文由作者按照 CC BY 4.0 进行授权