我一直在嘗試獲取基于類的串列視圖來顯示用戶帳戶(申請人)下的所有條目,但是在加載頁面時出現以下錯誤:
視圖 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 App 的 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')
uj5u.com熱心網友回復:
如果當前登錄用戶的申請人不存在,則您的 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')
uj5u.com熱心網友回復:
在ListView 過濾器上的 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')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/420716.html
標籤:
