创建Django3项目

1.djang-admin startproject test_pro

2.cd test_pro

3.mkdir apps

4.cd apps && python ../manage.py startapp app01

5.追加项目settings.py 中的导包路径,方便注册app
import sys, os
sys.path.insert(0, os.path.join(BASE_DIR,"apps"))
INSTALLED_APPS = [
    ...
    'app01',
]

6.app01中创建urls.py

7.项目的urls.py中引用app01的urls
from django.conf.urls import url, include

urlpatterns = [
    url(r'^app01/', include(('apps.app01.urls', 'app01'), namespace='app01')),
]

8.在app01中的views.py中定义视图类
from django.views import View
from django.http import HttpResponse

class Testapp01(View):

    def get(self, request):
        return HttpResponse("hello test.")

9.应用中的urls.py中
from django.urls import path
from .views import *

urlpatterns = [
    path('test/', Testapp01.as_view(), name='testapp01')
]

10.启动django,进行测试
http://127.0.0.1/app01/test/

11.目录结构
PS E:\DjangoProject\test_pro> tree /F
卷 文档 的文件夹 PATH 列表
卷序列号为 7AAC-225C
E:.
│  db.sqlite3
│  manage.py
│
├─.idea
│      encodings.xml
│      misc.xml
│      modules.xml
│      test_pro.iml
│      workspace.xml
│
├─apps
│  ├─app01
│  │  │  admin.py
│  │  │  apps.py
│  │  │  models.py
│  │  │  tests.py
│  │  │  urls.py
│  │  │  views.py
│  │  │  __init__.py
│  │  │
│  │  ├─migrations
│  │  │  │  __init__.py
│  │  │  │
│  │  │  └─__pycache__
│  │  │          __init__.cpython-37.pyc
│  │  │
│  │  └─__pycache__
│  │          admin.cpython-37.pyc
│  │          apps.cpython-37.pyc
│  │          models.cpython-37.pyc
│  │          urls.cpython-37.pyc
│  │          views.cpython-37.pyc
│  │          __init__.cpython-37.pyc
│  │
│  └─__pycache__
└─test_pro
    │  asgi.py
    │  settings.py
    │  urls.py
    │  wsgi.py
    │  __init__.py
    │
    └─__pycache__
            settings.cpython-37.pyc
            urls.cpython-37.pyc
            wsgi.cpython-37.pyc
            __init__.cpython-37.pyc
上一篇:Django基础4


下一篇:Python+实例解析Web页面元素提取