Django网站开发技术的应用(理论篇)

概述

Django提供了许多功能。比如在安全方面上Django提供了csrf防护机制以防止跨域脚本攻击、使用身份验证机制以防止未授权的登录等等。在数据库方面上Django提供了orm(面向对象的数据库访问技术)方便了对数据库的操作。此外Django还提供自定义模板、缓存技术等功能。使得使用Django开发网站的效率大大增加。类似于springboot框架,Django也可以在setting.py里添加若干配置如静态资源的位置、缓存、数据库连接配置等等。以下是一个setting.py的样例。大家可以在评论区大致猜猜这个Django项目里用到了哪些技术要点。

python 复制代码
"""
Django settings for WashingServiceManagementSystem project.

Generated by 'django-admin startproject' using Django 5.0.6.

For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
import channels.sessions
import django.contrib.messages.storage.cookie
import django_redis.client

BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-u1=23_h&2v5s0w#21cod&++ui8s%$yb5^(_9c+69c0iz^rolf2'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'daphne',
    'django.contrib.staticfiles',
    'channels',
    'services'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'WashingServiceManagementSystem.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates']
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'WashingServiceManagementSystem.wsgi.application'

ASGI_APPLICATION = 'WashingServiceManagementSystem.asgi.application'

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            'hosts': ["redis://localhost:6379/0"]
        }
    }
}
# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'

SESSION_COOKIE_NAME = 'py_session'

SESSION_COOKIE_AGE = 60 * 60 * 24

SESSION_EXPIRE_AT_BROWSER_CLOSE = True

CACHES={
    'default':{
        'BACKEND':'django_redis.cache.RedisCache',
        'LOCATION':'redis://127.0.0.1:6397/1'
    }
}

使用的第三方类库

Django还可以与其它第三方库配合使用。具体使用类库以及作用如下所示:

  • 网络爬虫:requests、requests-cache、urllib3、scrapy等等。
  • 数据处理:numpy、bs4、AdroitFisherman、re、xpath、json等等。
  • 网络通信:channels等等。
  • 多并发与异步:multiprocess、threading、asgiref、asyncio等等。
  • 底层调用:python C 扩展、ctypes等等。
  • 系统应用:sys、os等等。
  • etc

特性

django支持用户身份验证、使用自定义路径转换器、自定义标签、自定义过滤器、自定义代理类模型等等。与以上类库配合使用再结合vue、react等前端框架使得开发多了一些技术选择和逻辑扩充。

相关推荐
萌动的小火苗30 分钟前
1、python基础面试题
java·开发语言·python
运维行者_40 分钟前
网络监控与ITSM集成:从告警到工单,实现运维自动化闭环
开发语言·网络·分布式·后端·架构·flask·php
子兮曰43 分钟前
解剖 Claude Code:从入口架构逆向工程看 AI 编程工具的信任边界设计
前端·后端·claude
颜进强1 小时前
前端看后端 15:什么是 DNS?
前端·后端·ai编程
文艺理科生1 小时前
LangChain.js-v1-记忆管理最佳实践
前端·javascript·后端
sunywz1 小时前
【从零搭建物联网智能充电桩系统】2、自定义二进制协议:设备为什么不用 JSON?
python·物联网·json
小叮当爱咖啡1 小时前
Day3.参数+Prompt三板斧
开发语言·python·prompt
子兮曰1 小时前
Elysia vs Hono 完整性能对比:Bun 专属优化还是跨平台通用,2026 实测数据给出的答案
前端·后端·typescript
菜冻鱼1 小时前
Python-pandas-索引与筛选
开发语言·笔记·python·numpy·pandas·学习方法
小满zs2 小时前
Go语言第七章(Map)
后端·go