首页 > 解决方案 > Django - ListView url 未连接到所需视图

问题描述

我是 Django 新手,我的项目的某个部分遇到了困难,我希望有人能提供帮助。

我的 views.py 文件中有两个 ListView,我希望它们的工作方式类似于已发布/草稿的帖子(我实际上使用的是经过清理和未经清理的报告)。目前,每次我尝试访问“未消毒”列表视图 (unsanitised_list.html) 时,它只会将我引导到已消毒列表视图 (intelreport_list.html)

视图.py:

class IntelReportListView(ListView):
  model = IntelReport
  context_object_name = 'all_logs'

  def get_queryset(self):
    return IntelReport.objects.filter(create_date__lte=timezone.now()).order_by('-create_date')



class UnsanitisedListView(LoginRequiredMixin, ListView):
    login_url = '/login/'
    redirect_field_name = 'intel_db/unsanitised_list.html'
    
    model = IntelReport

    def get_queryset(self):
      return IntelReport.objects.filter(sanitised__isnull=True).order_by('-create_date')

模型.py

class IntelReport(models.Model):
    gpms_choices = (
      ***REDACTED***
    )
    gpms = models.CharField(max_length=20, blank=True, null=True, choices=gpms_choices)
    
    officer = models.CharField(max_length=50)
    create_date = models.DateTimeField(auto_now_add=True)
    sanitised = models.BooleanField(default=False)

    source_eval_choices = (
      ***REDACTED****
    )
    source_eval = models.CharField(max_length=50, blank=True, null=True, choices=source_eval_choices)
    
    intel_eval_choices = (
      ***REDACTED***
    )
    intel_eval = models.CharField(max_length=100, blank=True, null=True, choices=intel_eval_choices)

    report = models.TextField(max_length=5000, blank=True, null=True)

    def sanitised_log(self):
      self.sanitised = True
      self.save()

    def get_absolute_url(self):
      return reverse('log_details', kwargs={'pk':self.pk})

    def __str__(self):
      return str(self.pk)

网址.py

从 django.urls 导入路径从 intel_db 导入视图

urlpatterns = [
    path('welcome/', views.AboutView.as_view(), name='about'),
    path('logs/', views.IntelReportListView.as_view(), name='log_list'),
    path('logs/<int:pk>/', views.IntelReportDetailView.as_view(), name='log_detail'),
    path('logs/new_log/', views.IntelReportCreateView.as_view(), name='new_log'),
    path('unsanitised/', views.UnsanitisedListView.as_view(), name='unsanitised'),
    path('logs/<int:pk>/sanitise_log/', views.sanitsed_report, name='sanitised_report'),
]

在我的登录页面 (landing.html) 上,这是我用来尝试访问 unsanitised_list.html 的链接:

      **<a href="{% url 'unsanitised' %}">**
        

我无法弄清楚为什么它一直将我重定向到 intelreport_lists.html(已清理的日志)而不是 unsanitised_list.html(未清理的日志)。

我希望我不仅错过了一些非常简单的东西,而且我已经完成了它并无数次地尝试重新编写它并且无法正确地完成它。

我希望这是足够的信息,任何帮助将不胜感激。

干杯

标签: pythondjangodjango-modelsdjango-viewsdjango-templates

解决方案


您只需要template_name在扩展时覆盖ListView。我的意思是像这样更新你的IntelReportListViewUnsanitisedListView

class IntelReportListView(ListView):
  model = IntelReport
  context_object_name = 'all_logs'
  template_name = 'YOUR_APP_NAME/intelreport_list.html'

  def get_queryset(self):
    return IntelReport.objects.filter(create_date__lte=timezone.now()).order_by('-create_date')



class UnsanitisedListView(LoginRequiredMixin, ListView):
    login_url = '/login/'
    redirect_field_name = 'intel_db/unsanitised_list.html'
    template_name = 'YOUR_APP_NAME/unsanitised_list.html'
    model = IntelReport

    def get_queryset(self):
      return IntelReport.objects.filter(sanitised__isnull=True).order_by('-create_date')

如果您有兴趣了解它为什么重定向到intelreport_list.html而不是unsanitised_list.html,那么无论何时扩展它时,默认情况下ListView都会查找您在列表视图中使用的模型的名称(小写)。既然你已经使用inside ,它会重定向到MODEL_NAME_list.htmlMODEL_NAMEmodel = IntelReportUnsanitisedListViewintelreport_lists.html


推荐阅读