web project (1st day)

2024-04-22 23:18
文章标签 web day project 1st

本文主要是介绍web project (1st day),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

what should we do before start our web project

First–install what we need

Ensure which technologies will be used
think about all of them, such as DATABASE, CACHE, WEB FRAMEWORK, CELERY …
so: In python there is we need:
pip install Django
pip install django_redis
pip install redis
pip install celery
pip install pymysql
pip install pillow ===> for image code verification
…. that is now we need, and than we can extension them

Second–Structure is important than you think

Now, We need talk about the Django project
what application will be support?
what we do is good for the project? maybe docs?log?scripts?
how to manage the project?
how to realize ‘celery’?
….
think above all of them, we can deal with them like this:
│ ├── celery_tasks # execute asynchronous tasks
│ ├── docs # project docs
│ ├── logs # project exception log
│ │ └── meiduo.log
│ ├── manage.py
│ └── meiduo_mall
│ ├── apps # applications moudle
│ ├── libs # Third-party libraries
│ ├── settings # online or develop setting
│ │ ├── dev.py
│ │ ├── init.py
│ │ ├── prod.py
│ │ └── pycache
│ │ ├── dev.cpython-36.pyc
│ │ └── init.cpython-36.pyc
│ ├── settings_old.py
│ ├── urls.py
│ ├── utils # SDK support
│ │ ├── exceptions.py
│ │ └── init.py
│ └── wsgi.py

Third–Setting for better programing environment

questions:
how to sava exceptions log?
how connect databases?
how to realize asynchronous tasks?
how to location project easily?
how to handler exception?

now, deal with all questions?
1. sys.path

sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
  1. database setting ===> it’s easy
  2. Setting django-redis for asynchronous tasks
ACHES = {"default": {"BACKEND": "django_redis.cache.RedisCache","LOCATION": "redis://127.0.0.1:6379/0","OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient",}},"session": {"BACKEND": "django_redis.cache.RedisCache","LOCATION": "redis://127.0.0.1:6379/1","OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient",}}
}
SESSION_ENGINE = 'django.contrib.sessions.backends.db' # 数据库引擎设置
SESSION_CACHE_ALIAS= 'session'  # 使用的缓存数据库
  1. logging recording
LOGGING = {'version': 1,# 是否禁止其他的logging功能'disable_existing_loggers': False,# 格式化'formatters': {# 详细'verbose': {'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s'},# 简单'simple': {'format': '%(levelname)s %(module)s %(lineno)d %(message)s'},},# 过滤器'filters': {'require_debug_true': {'()': 'django.utils.log.RequireDebugTrue',},},# 处理方式'handlers': {# 解释器'console': {'level': 'DEBUG','filters': ['require_debug_true'],'class': 'logging.StreamHandler','formatter': 'simple'},# 保存到文件'file': {'level': 'INFO','class': 'logging.handlers.RotatingFileHandler','filename': os.path.join(os.path.dirname(BASE_DIR), "logs/meiduo.log"),  # 日志文件的位置'maxBytes': 300 * 1024 * 1024,'backupCount': 10,'formatter': 'verbose'},},# 定义日志器'loggers': {'django': {  # 定义了一个名为django的日志器'handlers': ['console', 'file'],'propagate': True,},}
}
  1. handler exception
    “`
    from django.db import DatabaseError
    from redis.exceptions import RedisError
    from rest_framework.response import Response
    from rest_framework.views import exception_handler as drf_exception_handler
    from rest_framework import status
    import logging

logger = logging.getLogger(‘django’)

def exception_handler(exc, context):
“””
修改rest_framework 中的异常处理 补充redis和数据库处理
:param exc:异常
:param context:异常发现时的上下文 context[‘view’] 可以访问发生异常的视图
:return:
“”“

# Call REST framework's default exception handler first,
# to get the standard error response.
# print('exc', exc)
# print('context', context)
response = drf_exception_handler(exc, context)
view = context['view']# Now add the HTTP status code to the response.
if response is None:# 查看是否存在数据库异常 以及 Redis异常if isinstance(response, DatabaseError) or isinstance(response, RedisError):logger.error('[%s] %s' % (view, exc))response = Response({"message": "服务器内部错误"}, status=status.HTTP_507_INSUFFICIENT_STORAGE)return response

“`
by the way, there you should setting REST_FRAMEWORK in setting.py

这篇关于web project (1st day)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/927143

相关文章

Java Web指的是什么

Java Web指的是使用Java技术进行Web开发的一种方式。Java在Web开发领域有着广泛的应用,主要通过Java EE(Enterprise Edition)平台来实现。  主要特点和技术包括: 1. Servlets和JSP:     Servlets 是Java编写的服务器端程序,用于处理客户端请求和生成动态网页内容。     JSP(JavaServer Pages)

day-51 合并零之间的节点

思路 直接遍历链表即可,遇到val=0跳过,val非零则加在一起,最后返回即可 解题过程 返回链表可以有头结点,方便插入,返回head.next Code /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}*

BUUCTF靶场[web][极客大挑战 2019]Http、[HCTF 2018]admin

目录   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 [web][HCTF 2018]admin 考点:弱密码字典爆破 四种方法:   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 访问环境 老规矩,我们先查看源代码

EasyPlayer.js网页H5 Web js播放器能力合集

最近遇到一个需求,要求做一款播放器,发现能力上跟EasyPlayer.js基本一致,满足要求: 需求 功性能 分类 需求描述 功能 预览 分屏模式 单分屏(单屏/全屏) 多分屏(2*2) 多分屏(3*3) 多分屏(4*4) 播放控制 播放(单个或全部) 暂停(暂停时展示最后一帧画面) 停止(单个或全部) 声音控制(开关/音量调节) 主辅码流切换 辅助功能 屏

9.8javaweb项目总结

1.主界面用户信息显示 登录成功后,将用户信息存储在记录在 localStorage中,然后进入界面之前通过js来渲染主界面 存储用户信息 将用户信息渲染在主界面上,并且头像设置跳转,到个人资料界面 这里数据库中还没有设置相关信息 2.模糊查找 检测输入框是否有变更,有的话调用方法,进行查找 发送检测请求,然后接收的时候设置最多显示四个类似的搜索结果

JavaWeb【day09】--(Mybatis)

1. Mybatis基础操作 学习完mybatis入门后,我们继续学习mybatis基础操作。 1.1 需求 需求说明: 根据资料中提供的《tlias智能学习辅助系统》页面原型及需求,完成员工管理的需求开发。 通过分析以上的页面原型和需求,我们确定了功能列表: 查询 根据主键ID查询 条件查询 新增 更新 删除 根据主键ID删除 根据主键ID批量删除

利用Django框架快速构建Web应用:从零到上线

随着互联网的发展,Web应用的需求日益增长,而Django作为一个高级的Python Web框架,以其强大的功能和灵活的架构,成为了众多开发者的选择。本文将指导你如何从零开始使用Django框架构建一个简单的Web应用,并将其部署到线上,让世界看到你的作品。 Django简介 Django是由Adrian Holovaty和Simon Willison于2005年开发的一个开源框架,旨在简

web群集--nginx配置文件location匹配符的优先级顺序详解及验证

文章目录 前言优先级顺序优先级顺序(详解)1. 精确匹配(Exact Match)2. 正则表达式匹配(Regex Match)3. 前缀匹配(Prefix Match) 匹配规则的综合应用验证优先级 前言 location的作用 在 NGINX 中,location 指令用于定义如何处理特定的请求 URI。由于网站往往需要不同的处理方式来适应各种请求,NGINX 提供了多种匹

Linux基础入门 --9 DAY

文本处理工具之神vim         vi和vim简介 一、vi编辑器 vi是Unix及类Unix系统(如Linux)下最基本的文本编辑器,全称为“visual interface”,即视觉界面。尽管其名称中包含“visual”,但vi编辑器实际上工作在字符模式下,并不提供图形界面。vi编辑器以其强大的功能和灵活性著称,是Linux系统中不可或缺的工具之一。 vi编辑器具有三种主要的工作模

构建高性能WEB之HTTP首部优化

0x00 前言 在讨论浏览器优化之前,首先我们先分析下从客户端发起一个HTTP请求到用户接收到响应之间,都发生了什么?知己知彼,才能百战不殆。这也是作为一个WEB开发者,为什么一定要深入学习TCP/IP等网络知识。 0x01 到底发生什么了? 当用户发起一个HTTP请求时,首先客户端将与服务端之间建立TCP连接,成功建立连接后,服务端将对请求进行处理,并对客户端做出响应,响应内容一般包括响应