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


    概述

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

    1. """
    2. Django settings for WashingServiceManagementSystem project.
    3. Generated by 'django-admin startproject' using Django 5.0.6.
    4. For more information on this file, see
    5. https://docs.djangoproject.com/en/5.0/topics/settings/
    6. For the full list of settings and their values, see
    7. https://docs.djangoproject.com/en/5.0/ref/settings/
    8. """
    9. from pathlib import Path
    10. # Build paths inside the project like this: BASE_DIR / 'subdir'.
    11. import channels.sessions
    12. import django.contrib.messages.storage.cookie
    13. import django_redis.client
    14. BASE_DIR = Path(__file__).resolve().parent.parent
    15. # Quick-start development settings - unsuitable for production
    16. # See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
    17. # SECURITY WARNING: keep the secret key used in production secret!
    18. SECRET_KEY = 'django-insecure-u1=23_h&2v5s0w#21cod&++ui8s%$yb5^(_9c+69c0iz^rolf2'
    19. # SECURITY WARNING: don't run with debug turned on in production!
    20. DEBUG = True
    21. ALLOWED_HOSTS = ['*']
    22. # Application definition
    23. INSTALLED_APPS = [
    24. 'django.contrib.admin',
    25. 'django.contrib.auth',
    26. 'django.contrib.contenttypes',
    27. 'django.contrib.sessions',
    28. 'django.contrib.messages',
    29. 'daphne',
    30. 'django.contrib.staticfiles',
    31. 'channels',
    32. 'services'
    33. ]
    34. MIDDLEWARE = [
    35. 'django.middleware.security.SecurityMiddleware',
    36. 'django.contrib.sessions.middleware.SessionMiddleware',
    37. 'django.middleware.common.CommonMiddleware',
    38. 'django.middleware.csrf.CsrfViewMiddleware',
    39. 'django.contrib.auth.middleware.AuthenticationMiddleware',
    40. 'django.contrib.messages.middleware.MessageMiddleware',
    41. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    42. ]
    43. ROOT_URLCONF = 'WashingServiceManagementSystem.urls'
    44. TEMPLATES = [
    45. {
    46. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
    47. 'DIRS': [BASE_DIR / 'templates']
    48. ,
    49. 'APP_DIRS': True,
    50. 'OPTIONS': {
    51. 'context_processors': [
    52. 'django.template.context_processors.debug',
    53. 'django.template.context_processors.request',
    54. 'django.contrib.auth.context_processors.auth',
    55. 'django.contrib.messages.context_processors.messages',
    56. ],
    57. },
    58. },
    59. ]
    60. WSGI_APPLICATION = 'WashingServiceManagementSystem.wsgi.application'
    61. ASGI_APPLICATION = 'WashingServiceManagementSystem.asgi.application'
    62. CHANNEL_LAYERS = {
    63. 'default': {
    64. 'BACKEND': 'channels_redis.core.RedisChannelLayer',
    65. 'CONFIG': {
    66. 'hosts': ["redis://localhost:6379/0"]
    67. }
    68. }
    69. }
    70. # Database
    71. # https://docs.djangoproject.com/en/5.0/ref/settings/#databases
    72. DATABASES = {
    73. 'default': {
    74. 'ENGINE': 'django.db.backends.sqlite3',
    75. 'NAME': BASE_DIR / 'db.sqlite3',
    76. }
    77. }
    78. # Password validation
    79. # https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
    80. AUTH_PASSWORD_VALIDATORS = [
    81. {
    82. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    83. },
    84. {
    85. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    86. },
    87. {
    88. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    89. },
    90. {
    91. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    92. },
    93. ]
    94. # Internationalization
    95. # https://docs.djangoproject.com/en/5.0/topics/i18n/
    96. LANGUAGE_CODE = 'en-us'
    97. TIME_ZONE = 'UTC'
    98. USE_I18N = True
    99. USE_TZ = True
    100. # Static files (CSS, JavaScript, Images)
    101. # https://docs.djangoproject.com/en/5.0/howto/static-files/
    102. STATIC_URL = 'static/'
    103. # Default primary key field type
    104. # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
    105. DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
    106. SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
    107. SESSION_COOKIE_NAME = 'py_session'
    108. SESSION_COOKIE_AGE = 60 * 60 * 24
    109. SESSION_EXPIRE_AT_BROWSER_CLOSE = True
    110. CACHES={
    111. 'default':{
    112. 'BACKEND':'django_redis.cache.RedisCache',
    113. 'LOCATION':'redis://127.0.0.1:6397/1'
    114. }
    115. }

    使用的第三方类库

            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等前端框架使得开发多了一些技术选择和逻辑扩充。

  • 相关阅读:
    [JavaScript]面对对象编程,构造函数,原型
    C语言每日一题(10):无人生还
    基于SPringBoot的药品管理系统
    dask读取sql数据:MySQL
    完全背包问题--找零钱
    操作系统内存管理
    第六章《类的高级特性》第5节:接口
    定制 ElementPlus 主题
    Windows网络「SSL错误问题」及解决方案
    [Genode] ARM TrustZone
  • 原文地址:https://blog.csdn.net/weixin_55094971/article/details/139955850