Python Django:几个路径,一个列表视图,不同的模板?

Python Django:几个路径,一个列表视图,不同的模板?,python,django,Python,Django,请帮帮我。我希望在url.py中有多个路径(比如a、b)。但我只希望有一个ListView,当我访问url时,此ListView需要将我引导到不同的html文件(访问“a/”时为a.html,访问“b/”时为b.html) 目前,我对每个路径使用不同的ListView(aListView和bListView),即使模型是相同的。但这似乎违反了“不要重复自己”的规则。代码看起来很糟糕 所以问题是如何在一个ListView中定义多个不同的模板 下面是我目前的思维导图。多谢各位 在我看来,在您的情况下

请帮帮我。我希望在url.py中有多个路径(比如a、b)。但我只希望有一个ListView,当我访问url时,此ListView需要将我引导到不同的html文件(访问“a/”时为a.html,访问“b/”时为b.html)

目前,我对每个路径使用不同的ListView(aListView和bListView),即使模型是相同的。但这似乎违反了“不要重复自己”的规则。代码看起来很糟糕

所以问题是如何在一个ListView中定义多个不同的模板

下面是我目前的思维导图。多谢各位


在我看来,在您的情况下,拥有两个独立的列表视图是正确的方法。您有两个不同的URL和两个不同的模板,因此您有两个不同的视图。我不认为它违反了DRY原则,因为公共逻辑是由可重用的
ListView
类抽象的。

下面是一个示例,说明如何在多个模板中定义指向一个ListView的多个路径:

让我们假设我的应用程序名是
example
,我的项目名是
test_project
;我的设置如下:

测试项目/url.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    ...,  # my project urls
    path('', include('example.urls'))
]
from django.urls import path
from example import views

app_name = 'example'
urlpatterns = [
    path('a/', views.GenericListView.as_view(), name='a-url'),
    path('b/', views.GenericListView.as_view(), name='b-url')
]
from django.views.generic.list import ListView
from django.urls import reverse


class GenericListView(ListView):
    a_template = 'a.html'
    b_template = 'b.html'

    def get_template_names(self, *args, **kwargs):
        # Check if the request path is the path for a-url in example app
        if self.request.path == reverse('example:a-url'):
            return [self.a_template]  # Return a list that contains "a.html" template name
        return [self.b_template]  # else return "b.html" template name

    def get_queryset(self, **kwargs):
        # For this example i'm returning [] as queryset for both a/ and b/ routes
        if self.request.path == reverse('example:a-url'):
            return []  # Return whatever queryset for a/ route
        return []  # Return whatever queryset for b/ route
示例/url.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    ...,  # my project urls
    path('', include('example.urls'))
]
from django.urls import path
from example import views

app_name = 'example'
urlpatterns = [
    path('a/', views.GenericListView.as_view(), name='a-url'),
    path('b/', views.GenericListView.as_view(), name='b-url')
]
from django.views.generic.list import ListView
from django.urls import reverse


class GenericListView(ListView):
    a_template = 'a.html'
    b_template = 'b.html'

    def get_template_names(self, *args, **kwargs):
        # Check if the request path is the path for a-url in example app
        if self.request.path == reverse('example:a-url'):
            return [self.a_template]  # Return a list that contains "a.html" template name
        return [self.b_template]  # else return "b.html" template name

    def get_queryset(self, **kwargs):
        # For this example i'm returning [] as queryset for both a/ and b/ routes
        if self.request.path == reverse('example:a-url'):
            return []  # Return whatever queryset for a/ route
        return []  # Return whatever queryset for b/ route
模板/a.html

This is A file
This is B file
模板/b.html

This is A file
This is B file
示例/views.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    ...,  # my project urls
    path('', include('example.urls'))
]
from django.urls import path
from example import views

app_name = 'example'
urlpatterns = [
    path('a/', views.GenericListView.as_view(), name='a-url'),
    path('b/', views.GenericListView.as_view(), name='b-url')
]
from django.views.generic.list import ListView
from django.urls import reverse


class GenericListView(ListView):
    a_template = 'a.html'
    b_template = 'b.html'

    def get_template_names(self, *args, **kwargs):
        # Check if the request path is the path for a-url in example app
        if self.request.path == reverse('example:a-url'):
            return [self.a_template]  # Return a list that contains "a.html" template name
        return [self.b_template]  # else return "b.html" template name

    def get_queryset(self, **kwargs):
        # For this example i'm returning [] as queryset for both a/ and b/ routes
        if self.request.path == reverse('example:a-url'):
            return []  # Return whatever queryset for a/ route
        return []  # Return whatever queryset for b/ route

有关更多信息,您可以访问

如果我答对了您的问题,您可以在models.py文件(having class等)中创建与db相关的内容,然后在views.py中创建一个函数,该函数可以进一步使用名称对其进行分类。现在,在应用程序url.py中添加2个条目,1个用于a.html,另一个用于b.html,其函数名与我们在views.py中创建的函数名相同。这样,您只创建了一个由多个页面调用的函数(基于它们的html模板值)。请让我知道,如果这对你有帮助,那么它将成为一个完整的答案。这是一个非常清楚的答案,文档链接也很有帮助,我几乎做了同样的事情,但在视图中有HttpResponse。py用于显示示例:)如果我能完成它,将发布,干杯并感谢分享漂亮的答案。@RavinderSingh13谢谢。请参阅我的上一次更新,了解针对URL的更通用测试。别忘了接受回答你问题的答案。@ChihebNexus greatt。这就是我要找的。这有助于我的一天!!非常感谢。