1.登录时 post请求变成get请求
1.问题描述
1.事情发生的条件
- 接口
path('login/', MyTokenObtainPairView.as_view())
- 请求方式是一个post请求
settings.py
配置文件中APPEND_SLASH = True
(这个作用是当请求路径最后是 / ,如果请求时没有斜杠自动补全斜杠)
2. 发起请求报错
发起请求,请求路径
http://admin.business.infinities.com.cn/login
首先,django中间件 CommonMiddleware 会补齐路径中的/ ,然后请求重定向到 路径 http://admin.business.infinities.com.cn/login/,并且把请求方式改成GET方法,之后报405的错误,请求方式不对。
2.问题原因
settings
中设置 APPEND_SLASH = True
当请求url
最后是一个/
但是我们请求时没有加的话,如果设置APPEND_SLASH = True
,会自动给我补上 /
具体解释看源码:
class CommonMiddleware(MiddlewareMixin):
response_redirect_class = HttpResponsePermanentRedirect
def process_request(self, request):
"""
Check for denied User-Agents and rewrite the URL based on
settings.APPEND_SLASH and settings.PREPEND_WWW
"""
# Check for denied User-Agents
user_agent = request.META.get('HTTP_USER_AGENT')
if user_agent is not None:
for user_agent_regex in settings.DISALLOWED_USER_AGENTS:
if user_agent_regex.search(user_agent):
raise PermissionDenied('Forbidden user agent')
# Check for a redirect based on settings.PREPEND_WWW
host = request.get_host()
must_prepend = settings.PREPEND_WWW and host and not host.startswith('www.')
redirect_url = ('%s://www.%s' % (request.scheme, host)) if must_prepend else ''
# Check if a slash should be appended
if self.should_redirect_with_slash(request):
path = self.get_full_path_with_slash(request)
else:
path = request.get_full_path()
# Return a redirect if necessary
if redirect_url or path != request.get_full_path():
redirect_url += path
return self.response_redirect_class(redirect_url)
def should_redirect_with_slash(self, request):
"""
Return True if settings.APPEND_SLASH is True and appending a slash to
the request path turns an invalid path into a valid one.
"""
if settings.APPEND_SLASH and not request.path_info.endswith('/'):
urlconf = getattr(request, 'urlconf', None)
return (
not is_valid_path(request.path_info, urlconf) and
is_valid_path('%s/' % request.path_info, urlconf)
)
return False
def get_full_path_with_slash(self, request):
"""
Return the full path of the request with a trailing slash appended.
Raise a RuntimeError if settings.DEBUG is True and request.method is
POST, PUT, or PATCH.
"""
new_path = request.get_full_path(force_append_slash=True)
# Prevent construction of scheme relative urls.
new_path = escape_leading_slashes(new_path)
if settings.DEBUG and request.method in ('POST', 'PUT', 'PATCH'):
raise RuntimeError(
"You called this URL via %(method)s, but the URL doesn't end "
"in a slash and you have APPEND_SLASH set. Django can't "
"redirect to the slash URL while maintaining %(method)s data. "
"Change your form to point to %(url)s (note the trailing "
"slash), or set APPEND_SLASH=False in your Django settings." % {
'method': request.method,
'url': request.get_host() + new_path,
}
)
return new_path
首先在process_request
函数中检查是否需要加后缀:
#Check if a slash should be appended if self.should_redirect_with_slash(request): path = self.get_full_path_with_slash(request) else: path = request.get_full_path()
should_redirect_with_slash
的作用是:
def should_redirect_with_slash(self, request):
"""
如果 settings.APPEND_SLASH 是 True 并且加了斜杠之后能将无效路径转成有效
路径 就发返回 TRUE
"""
if settings.APPEND_SLASH and not request.path_info.endswith('/'):
urlconf = getattr(request, 'urlconf', None)
return (
not is_valid_path(request.path_info, urlconf) and
is_valid_path('%s/' % request.path_info, urlconf)
)
return False
is_valid_path是检验当前路径是否是有效路径
如果 settings.APPEND_SLASH 是 True 并且加了斜杠之后能将无效路径转成有效路径 就发返回 TRUE
之后程序进行到:
if redirect_url or patha != request.get_full_path():
redirect_url += path
return self.response_redirect_class(redirect_url)
重定向 新的加了斜杠的路径,此时请求方法变成 GET
3.解决方法
把请求路径的 / 去掉,改成:
re_path(r'^login$', MyTokenObtainPairView.as_view()),
2.redis远程连接出现问题
1.问题描述
redis安装在linux上,本地windows django中 python manage.py shell
中 连接 redis,一直报错:
redis.exceptions.ConnectionError: Error 10061 connecting to 172.25.102.72:4545. 由于目标计算机积极拒绝,无法连接。.
2.问题原因
-
可能是 redis服务器未启动
-
可能是protected-mode设置参数不对
3.解决方法
-
redis的bind 参数设置
在redis.conf中 bind的默认参数 是127.0.0.1 ,这样能防止redis服务暴露于危险的网络环境,但是只能本机的客户端访问,如果想要远程服务器连接到redis:
bind的以下三种设置都行:
-
第一种:bind 127.0.0.1 linux的本机ip
e.g. bind 127.0.0.1 172.25.102.72
-
第二种:bind 0.0.0.0
-
第三种:#bind 127.0.0.1 (注销bind)
第一、二种配置的是本地网卡
-
-
redis的protected-mode参数设置(protected-mode yes)–这时bind设置按照第一项三种bind连接方式任意一种均可
是否开启保护模式,默认开启。 强制外部访问时需要在配置中设立密码
如果关闭这个(protected-mode no),不需要设置密码,不安全
建议 protected-mode 设置成 yes 并在redis.conf中设立密码(requirepass 957417302hui)
-
redis的daemonize参数设置(daemonize yes)
1、daemonize介绍
- A、redis.conf配置文件中daemonize守护线程,默认是NO。
- B、daemonize是用来指定redis是否要用守护线程的方式启动。
2、daemonize 设置yes或者no区别
daemonize:yes
:redis采用的是单进程多线程的模式。当redis.conf中选项daemonize设置成yes时,代表开启守护进程模式。在该模式下,redis会在后台运行,并将进程pid号写入至redis.conf选项pidfile设置的文件中,此时redis将一直运行,除非手动kill该进程。daemonize:no
: 当daemonize选项设置成no时,当前界面将进入redis的命令行界面,exit强制退出或者关闭连接工具(putty,xshell等)都会导致redis进程退出。
建议设置成 yes
-
每次修改完配置均需要重启redis
-
先查redis的pid
$ ps aux|grep redis redis 74925 98.3 5.0 16944560 13333556 ? R 19:38 1:11 redis-rdb-bgsave 0.0.0.0:7001 [cluster] redis 75710 97.0 4.8 19931756 12899868 ? R 19:39 0:32 redis-rdb-bgsave 0.0.0.0:7000 [cluster] bigdata+ 76406 0.0 0.0 112708 984 pts/4 S+ 19:39 0:00 grep --color=auto redis polkitd 191064 0.1 0.0 55516 4248 ? Ssl Apr26 26:12 redis-server *:6379 redis 246367 8.4 4.8 19931752 12901412 ? Rl 2019 72730:27 redis-server 0.0.0.0:7000 [cluster]
-
然后杀掉
kill [pid号]
-
重启redis
$ /home/bigdataservice/wangqiang/redis-6.2.3/src/redis-server /home/bigdataservice/wangqiang/redis-6.2.3/redis.conf
-