欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > Django网站开发技术的应用(理论篇)

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

2024/10/25 13:25:44 来源:https://blog.csdn.net/weixin_55094971/article/details/139955850  浏览:    关键词:Django网站开发技术的应用(理论篇)

概述

        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.clientBASE_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 = TrueALLOWED_HOSTS = ['*']# Application definitionINSTALLED_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/#databasesDATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3','NAME': BASE_DIR / 'db.sqlite3',}
}# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validatorsAUTH_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 = TrueUSE_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-fieldDEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'SESSION_ENGINE = 'django.contrib.sessions.backends.cache'SESSION_COOKIE_NAME = 'py_session'SESSION_COOKIE_AGE = 60 * 60 * 24SESSION_EXPIRE_AT_BROWSER_CLOSE = TrueCACHES={'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等前端框架使得开发多了一些技术选择和逻辑扩充。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com