You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Django项目至cPanel后出现500内部服务器错误求助

问题

我将Django项目部署到cPanel平台后,通过RegisterView提交符合必填字段要求的JSON数据创建用户时,数据库已成功生成用户记录,但接口返回500内部服务器错误;通过后台管理面板创建用户时,同样会触发该错误。

RegisterView 代码

class RegisterView(APIView):
    """
    Register a new user.
    """
    permission_classes = [AllowAny]

    def post(self, request):
        """
        Getting the user's information for register.
        """
        try:
            email = request.data["email"]
            username = request.data["username"]
            password = request.data["password"]
            confirm_password = request.data["confirm_password"]
            full_name = request.data["full_name"]
            simple_description = request.data["simple_description"]
            biography = request.data["biography"]
            profile_picture = request.FILES.get("profile_picture")

            if User.objects.filter(email=email).exists():
                return Response(data={"error": "Email has been already used."},
                                status=status.HTTP_400_BAD_REQUEST)

            if User.objects.filter(username=username).exists():
                return Response(data={"error": "Username has been already used."},
                                status=status.HTTP_400_BAD_REQUEST)

            if password != confirm_password:
                return Response(data={"error": "Passwords do not match."},
                                status=status.HTTP_400_BAD_REQUEST)

            
            user = User.objects.create(
                username=username,
                email=email,
                password=password,
                full_name=full_name,
                simple_description=simple_description,
                biography=biography,
            )

            if profile_picture:
                user.profile_picture = profile_picture
                user.save()

            Notification.objects.create(
                author=user,
                title="Create account",
                description=CREATE_ACCOUNT_DESCRIPTION
            )

            return Response(data={'message': 'User has been successfully created.'},
                            status=status.HTTP_200_OK)
        
        except RequestException:
            return Response(data={"error": "An error occurred. Please check your internet connection."},
                            status=status.HTTP_503_SERVICE_UNAVAILABLE)

提交的JSON数据

{
    "email": "example@gmail.com",
    "password": "password123",
    "confirm_password": "password123",
    "full_name": "John Doe",
    "simple_description": "Tourist",
    "biography": "I love traveling.",
    "username": "johndoe"
}
问题分析与解决

核心问题1:密码未加密存储

Django的User模型(包括自定义User模型)不支持直接用明文密码赋值创建用户,User.objects.create(password=password)会把明文存入数据库,后续系统验证密码时会失败,触发500错误。

修复代码:
替换原有的用户创建逻辑,改用set_password()方法加密密码:

# 移除create中的password参数
user = User.objects.create(
    username=username,
    email=email,
    full_name=full_name,
    simple_description=simple_description,
    biography=biography,
)
# 单独设置加密密码并保存
user.set_password(password)
user.save()

核心问题2:异常捕获范围过窄

当前仅捕获RequestException,但创建用户、通知时可能抛出其他异常(比如CREATE_ACCOUNT_DESCRIPTION未定义、Notification模型字段错误、文件权限不足等),这些异常会直接触发500错误,无法进入自定义异常处理分支。

修复代码:
扩大捕获范围,同时记录具体错误(便于排查):

try:
    # 原有业务代码...
except RequestException:
    return Response(data={"error": "请求异常,请检查网络连接。"}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
except Exception as e:
    # 建议在日志中记录e的具体内容,方便定位问题
    return Response(data={"error": "创建用户时发生未知错误。"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

其他排查点

  1. 检查文件存储权限:如果涉及profile_picture上传,确保项目的MEDIA_ROOT目录在cPanel上有可写权限,否则保存用户时会抛出权限错误。
  2. 验证常量定义:确认CREATE_ACCOUNT_DESCRIPTION已正确定义,未定义会抛出NameError直接导致500错误。
  3. 查看Django日志:在cPanel中找到项目的错误日志,查看具体的500错误堆栈信息,这是定位问题最直接的方式。

内容的提问来源于stack exchange,提问作者nima_shadloo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 03:52:49