【问题标题】:List Class View didn't return an HttpResponse列表类视图未返回 HttpResponse
【发布时间】:2022-01-24 04:21:57
【问题描述】:

我一直在尝试获取基于类的列表视图来显示用户帐户(申请人)下的所有条目,但在加载页面时出现以下错误:

视图 jobassessment.views.view 没有返回 HttpResponse 对象。而是返回 None。

在我看来,URL 调度程序没有运行正确的视图,但这是我的整个网站和工作评估应用程序的 URL 文件,我似乎无法发现错误。

网站 URL.py:

urlpatterns = [
    path('admin/', admin.site.urls, name="admin"),
    path('accounts/', include('django.contrib.auth.urls'), name="accounts"),
    path('applicant/', include('userprofile.urls'), name="applicant"),
    path('assessments/', include('jobassessment.urls')),
]

JobAssessment 应用的 URL.py:

from django.urls import path
from . import views

urlpatterns = [
    path("", views.AssessmentListView.as_view(), name="assessment"),
]

这是我调用的 ListView:

class AssessmentListView(LoginRequiredMixin, generic.ListView):
    model = Assessment
    template_name ='assessments_index.html'
    paginate_by = 5
    def get(self, request, *args, **kwargs):
        # Ensure they have first created an Applicant Profile
        if not Applicant.objects.filter(user=self.request.user).exists():
            messages.info(request, "You must create a profile before you can view any assessments.")
            return redirect('profile_create_form') 

    def get_queryset(self):
        return Assessment.objects.all().filter(applicant=Applicant.objects.filter(user=self.request.user)).order_by('-assessment_stage')

【问题讨论】:

  • 遇到if not Applicant.objects.filter(user=self.request.user).exists():的else情况所以返回None,需要在else情况下编写逻辑返回响应类型

标签: python-3.x django django-views


【解决方案1】:

如果当前登录用户的申请人不存在,那么您的 if 条件不成立,因为没有其他人 部分在那里,所以视图没有返回 HttpResponse 。所以如果申请人存在请添加else部分并返回HttpResponse()

class AssessmentListView(LoginRequiredMixin, generic.ListView):
        model = Assessment
        template_name ='assessments_index.html'
        paginate_by = 5
        def get(self, request, *args, **kwargs):
            # Ensure they have first created an Applicant Profile
            if not Applicant.objects.filter(user=self.request.user).exists():
                messages.info(request, "You must create a profile before you can view any assessments.")
                return redirect('profile_create_form')
            else:
                return HttpResponse()  #<------ add corresponding HttpResponse if Applicant exists.

        def get_queryset(self):
            return Assessment.objects.all().filter(applicant=Applicant.objects.filter(user=self.request.user)).order_by('-assessment_stage')

【讨论】:

  • 谢谢,我可以使用return super().get(request, *args, **kwargs) 并解决了这个问题。
【解决方案2】:

按照ListView filter 上的django 文档,最好在get_queryset 内处理。所以对于你的情况,它会是这样的:

class AssessmentListView(LoginRequiredMixin, generic.ListView):
    model = Assessment
    template_name ='assessments_index.html'
    paginate_by = 5

    def get_queryset(self):
        # Ensure they have first created an Applicant Profile
        if not Applicant.objects.filter(user=self.request.user).exists():
            messages.info(request, "You must create a profile before you can view any assessments.")
            return redirect('profile_create_form') 
        else:
            return Assessment.objects.all().filter(applicant=Applicant.objects.filter(user=self.request.user)).order_by('-assessment_stage')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2014-12-03
    • 2021-06-22
    相关资源
    最近更新 更多