python - 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(未清理的日志)。
我希望我不仅错过了一些非常简单的东西,而且我已经完成了它并无数次地尝试重新编写它并且无法正确地完成它。
我希望这是足够的信息,任何帮助将不胜感激。
干杯
解决方案
您只需要template_name
在扩展时覆盖ListView
。我的意思是像这样更新你的IntelReportListView
,UnsanitisedListView
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.html
MODEL_NAME
model = IntelReport
UnsanitisedListView
intelreport_lists.html
推荐阅读
- c# - 检查是否有人在我的 Windows 窗体 C# 中仅单击一个图片框
- hybris - Hybris 后台菜单更改为 hMC 菜单
- reactjs - 如何防止网页样式受到注入 chrome 扩展样式的影响
- javascript - npm 安装的问题
- ios - iOS Unity 应用程序中的位置权限字符串
- python - Python中使用相同数据的三个子图
- javascript - 将调用的块相对于父块 JS 居中
- android-lifecycle - 我们应该在 Fragment/Activity 中注销 Flow 集合吗?
- c++ - C++ 使用 date.h 从当前时间减去天数
- android - Android onItemClick 方法不适用于 0 以外的 id 项