赞
踩
Django提供了许多功能。比如在安全方面上Django提供了csrf防护机制以防止跨域脚本攻击、使用身份验证机制以防止未授权的登录等等。在数据库方面上Django提供了orm(面向对象的数据库访问技术)方便了对数据库的操作。此外Django还提供自定义模板、缓存技术等功能。使得使用Django开发网站的效率大大增加。类似于springboot框架,Django也可以在setting.py里添加若干配置如静态资源的位置、缓存、数据库连接配置等等。以下是一个setting.py的样例。大家可以在评论区大致猜猜这个Django项目里用到了哪些技术要点。
- """
- 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还可以与其它第三方库配合使用。具体使用类库以及作用如下所示:
django支持用户身份验证、使用自定义路径转换器、自定义标签、自定义过滤器、自定义代理类模型等等。与以上类库配合使用再结合vue、react等前端框架使得开发多了一些技术选择和逻辑扩充。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。