社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

RedWheelbarrow

RedWheelbarrow 最近创建的主题
RedWheelbarrow 最近回复了
3 年前
回复了 RedWheelbarrow 创建的主题 » 使用nginx在Django-ALLOWED_主机中处理动态IP

我使用AWS EC2,通过健康检查,动态获取实例的ip,然后将其插入允许的_主机(我认为它也适用于Lightsail容器):

import requests

def get_instance_ip():
    try:
        ip = requests.get('http://169.254.169.254/latest/meta-data/local-ipv4').text
    except requests.exceptions.ConnectionError:
        return None
    return ip

AWS_IP = get_ec2_instance_ip()

if AWS_IP is not None:
    ALLOWED_HOSTS += [AWS_IP]

您还可以创建一个中间件,该中间件始终为健康检查使用的路径返回200状态代码(在之前插入自定义中间件) django.middleware.security.SecurityMiddleware 在里面 MIDDLEWARE Invalid HTTP_HOST header 错误)。

3 年前
回复了 RedWheelbarrow 创建的主题 » 在查询django ORM之前将字符串转换为int

试试这个:

from django.db.models.functions import Cast
from django.db.models import IntegerField
Entry.objects.annotate(maca_integer=Cast('maca', output_field=IntegerField())).filter(maca_integer__gte=90)
3 年前
回复了 RedWheelbarrow 创建的主题 » 语法“exp1<<variable<<exp2”在Python中是如何工作的?

<< 是一个轮班操作员,你需要使用 < <= :

a = 7
if 0 <= a <= 10:
    print('a is greater than or equal to 0 and less than or equal to 10')
else:
    print('a is not greater than or equal to 0 and less than or equal to 10')
3 年前
回复了 RedWheelbarrow 创建的主题 » 如何在Django Rest框架中更改前端序列化用户模型的字段名?

您需要传递一个名为 email 而不是 username 给你的 ModelBackend 子类:

class LoginSerializer(serializers.Serializer):
    username = serializers.CharField()
    password = serializers.CharField()

    def validate(self, data):
        user = authenticate(**{'email': data['username'], 'password': data['password']})
        if user and user.is_active:
            return user
        raise serializers.ValidationError('Incorrect Credentials Passed.')