0
点赞
收藏
分享

微信扫一扫

Django入门

Sky飞羽 2022-02-27 阅读 54

参考文章:

Writing your first Django app, part 1 | Django documentation | Django

安装django

python -m pip install Django

进入需要的目录,创建项目

django-admin startproject mysite

mysite:项目名称

目录结构

mysite/
    manage.py
    mysite/
        __init__.py    一个空文件,告诉Python该目录应被视为Python包
        settings.py    项目配置文件
        urls.py        url和函数的关系
        asgi.py        异步网络请求
        wsgi.py        同步网络请求

These files are:

  • The outer mysite/ root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
  • The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).
  • mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
  • mysite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.
  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

启动开发服务端

python manage.py runserver 0:8000

一个project可以包含多个app

在manage.py所在目录执行命令,创建名为polls的app

python manage.py startapp polls

目录结构为

polls/
    __init__.py
    admin.py               后台管理
    apps.py                app启动
    migrations/            数据库变更记录
        __init__.py
    models.py              对数据的操作
    tests.py               单元测试
    views.py               函数


首个view

打开polls/views.py,写入

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

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

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

在项目目录mysite下修改urls.py文件,导入django.urls.include,并插入include()

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

path()有4个参数,2个必须的route、view,2个可选的kwargs、name

path() 参数: route

route 是一个匹配 URL 的准则(类似正则表达式)。当 Django 响应一个请求时,它会从 urlpatterns 的第一项开始,按顺序依次匹配列表中的项,直到找到匹配的项。

这些准则不会匹配 GET 和 POST 参数或域名。例如,URLconf 在处理请求 https://www.example.com/myapp/ 时,它会尝试匹配 myapp/ 。处理请求 https://www.example.com/myapp/?page=3 时,也只会尝试匹配 myapp/

path() 参数: view

当 Django 找到了一个匹配的准则,就会调用这个特定的视图函数,并传入一个 HttpRequest 对象作为第一个参数,被“捕获”的参数以关键字参数的形式传入。稍后,我们会给出一个例子。

path() 参数: kwargs

任意个关键字参数可以作为一个字典传递给目标视图函数。本教程中不会使用这一特性。

path() 参数: name

为你的 URL 取名能使你在 Django 的任意地方唯一地引用它,尤其是在模板中。这个有用的特性允许你只改一个文件就能全局地修改某个 URL 模式。

当你了解了基本的请求和响应流程后,请阅读 教程的第 2 部分 开始使用数据库.

举报

相关推荐

0 条评论