Django应用部署Azure App Service后请求数据出现500服务器错误
Django部署Azure App Service Docker容器后500错误排查
本地运行正常的Django应用,部署到Azure App Service的Docker容器后,访问primaryexamsdata/接口返回500服务器错误。该接口在本地环境复制请求URL可正常响应,其他同配置视图工作正常。
相关代码
URLs.py
path('primaryexams/', TemplateView.as_view(template_name='xxxDB/primaryexams.html'), name='primaryExams'), path('primaryexamsdata/', views.PrimaryExamsView.as_view(), name='primaryexam_data'),
views.py
class PrimaryExamsView(generics.ListAPIView): serializer_class = PrimaryExamSerializer template_name='xxxDB/primaryexams.html' def get_queryset(self): return xxxPrimaryExamData.objects.all() def filter_for_datatable(self, queryset): # filtering search_query = self.request.query_params.get('search[value]') if search_query: lookups = Q(xxxid__first_name__icontains=search_query)|Q(xxxid__last_name__icontains=search_query)|Q(xxxid__xx_id__icontains=search_query) queryset = xxxPrimaryExamData.objects.filter(lookups) return queryset def list(self, request, *args, **kwargs): draw = request.query_params.get('draw') queryset = self.filter_queryset(self.get_queryset()) recordsTotal = queryset.count() filtered_queryset = self.filter_for_datatable(queryset) try: start = int(request.query_params.get('start')) except (ValueError, TypeError): start = 0 try: length = int(request.query_params.get('length')) except (ValueError, TypeError): length = 25 end = length + start serializer = self.get_serializer(filtered_queryset[start:end], many=True) response = { 'draw': draw, 'recordsTotal': recordsTotal, 'recordsFiltered': filtered_queryset.count(), 'data': serializer.data, } return Response(response)
serializers.py
class PrimaryExamSerializer(serializers.ModelSerializer): xxx_id = serializers.ReadOnlyField(source='xxxid.xxx_id') last_name = serializers.ReadOnlyField(source='xxxid.last_name') first_name = serializers.ReadOnlyField(source='xxxid.first_name') program_institution = serializers.ReadOnlyField(source='program_institution.institution_id') program_institution_name = serializers.ReadOnlyField(source='program_institution.institution_name') test_center_institution = serializers.ReadOnlyField(source='test_center_institution.institution_id', default='none') class Meta: model = AbnsPrimaryExamData fields = ( 'id','xxx_id','last_name','first_name','medical_school','program_institution','program_institution_name','graduation_year','test_center_institution' )
排查与解决方法
1. 修复视图类方法缩进错误
这是最可能的触发原因:原views.py中get_queryset、filter_for_datatable、list方法未缩进,不属于PrimaryExamsView类,导致运行时无法找到类方法,引发500错误。修正缩进后的代码如下:
class PrimaryExamsView(generics.ListAPIView): serializer_class = PrimaryExamSerializer template_name='xxxDB/primaryexams.html' def get_queryset(self): return xxxPrimaryExamData.objects.all() def filter_for_datatable(self, queryset): # filtering search_query = self.request.query_params.get('search[value]') if search_query: lookups = Q(xxxid__first_name__icontains=search_query)|Q(xxxid__last_name__icontains=search_query)|Q(xxxid__xx_id__icontains=search_query) queryset = xxxPrimaryExamData.objects.filter(lookups) return queryset def list(self, request, *args, **kwargs): draw = request.query_params.get('draw') queryset = self.filter_queryset(self.get_queryset()) recordsTotal = queryset.count() filtered_queryset = self.filter_for_datatable(queryset) try: start = int(request.query_params.get('start')) except (ValueError, TypeError): start = 0 try: length = int(request.query_params.get('length')) except (ValueError, TypeError): length = 25 end = length + start serializer = self.get_serializer(filtered_queryset[start:end], many=True) response = { 'draw': draw, 'recordsTotal': recordsTotal, 'recordsFiltered': filtered_queryset.count(), 'data': serializer.data, } return Response(response)
2. 查看Azure容器详细错误日志
- 登录Azure门户,进入对应App Service的日志流页面,查看实时错误输出,获取具体异常信息
- 临时开启Django的
DEBUG=True(生产环境务必关闭),并将ALLOWED_HOSTS设置为['xxxinternal.azurewebsites.net', 'localhost'],直接访问接口查看详细错误页面
3. 验证数据库连接与数据一致性
- 检查Azure容器中
settings.py的DATABASES配置,确认主机、端口、用户名、密码与本地一致,且Azure数据库防火墙允许App Service访问 - 验证Azure数据库中
xxxPrimaryExamData关联的外键数据(如xxxid、program_institution)是否存在,避免序列化时触发DoesNotExist异常
4. 排查序列化器潜在问题
- 对于
test_center_institution字段,若Azure数据库中该外键可能为NULL,建议将字段改为serializers.ReadOnlyField(source='test_center_institution.institution_id', allow_null=True),避免默认值'none'与字段类型不匹配 - 确认序列化器中
source指向的字段在关联模型中确实存在,比如xxxid.xxx_id是否是xxxid模型的有效字段
5. 核对环境变量配置
确保Azure容器中设置了与本地一致的环境变量,包括SECRET_KEY、数据库连接字符串、时区等,避免因环境差异引发错误
内容的提问来源于stack exchange,提问作者user1336827
相关产品推荐
相关产品推荐

