【问题标题】:URL-parameters and logic in Django class-based views (TemplateView)基于 Django 类的视图 (TemplateView) 中的 URL 参数和逻辑
【发布时间】:2013-03-23 03:47:10
【问题描述】:

我不清楚在 Django 1.5 中如何最好地访问基于类的视图中的 URL 参数。

考虑以下几点:

查看:

from django.views.generic.base import TemplateView


class Yearly(TemplateView):
    template_name = "calendars/yearly.html"

    current_year = datetime.datetime.now().year
    current_month = datetime.datetime.now().month

    def get_context_data(self, **kwargs):
        context = super(Yearly, self).get_context_data(**kwargs)
        context['current_year'] = self.current_year
        context['current_month'] = self.current_month
        return context

URLCONF:

from .views import Yearly


urlpatterns = patterns('',
    url(
        regex=r'^(?P<year>\d+)/$',
        view=Yearly.as_view(),
        name='yearly-view'
    ),
)

我想在我的视图中访问year 参数,所以我可以执行如下逻辑:

month_names = [
    "January", "February", "March", "April", 
    "May", "June", "July", "August", 
    "September", "October", "November", "December"
]

for month, month_name in enumerate(month_names, start=1):
    is_current = False
    if year == current_year and month == current_month:
        is_current = True
        months.append({
            'month': month,
            'name': month_name,
            'is_current': is_current
        })

如何最好地访问 CBV 中的 url 参数,例如上面的 TemplateView 的子类,以及理想情况下应该在哪里放置这样的逻辑,例如。在一个方法中?

【问题讨论】:

  • django2中有简单的extra_contextdict的选项,见here

标签: python django django-class-based-views


【解决方案1】:

要在基于类的视图中访问 url 参数,请使用 self.argsself.kwargs,这样您就可以通过 self.kwargs['year'] 访问它

【讨论】:

  • 是否正确理解我不应该像上面那样直接在视图中创建变量? (关于他们坚持不懈的一些事情)。另外我不明白我应该在哪里放置上述逻辑,例如。用什么方法?此外,当我在视图中执行year = self.kwargs['year'] 时,我得到NameError: self not defined
  • 从技术上讲,您不应该这样做,因为它们处于类级别并且是类变量。至于NameError,你想在哪里做year = self.kwargs['year']?您应该在方法中执行此操作,而不能在类级别执行此操作。例如,您使用的是 TemplateView,这意味着您将在 get_context_data 覆盖中执行逻辑。
  • 仅供参考:关于self.request、self.args等的文档可以在docs.djangoproject.com/en/1.10/topics/class-based-views/…找到
  • 如果你想在其他函数之外访问它,也可以在类中的def __init__(self):函数中进行。
【解决方案2】:

到目前为止,我只能从 get_queryset 方法中访问这些 url 参数,尽管我只尝试使用 ListView 而不是 TemplateView。我将使用 url 参数在对象实例上创建一个属性,然后在 get_context_data 中使用该属性来填充上下文:

class Yearly(TemplateView):
    template_name = "calendars/yearly.html"

    current_year = datetime.datetime.now().year
    current_month = datetime.datetime.now().month

    def get_queryset(self):
        self.year = self.kwargs['year']
        queryset = super(Yearly, self).get_queryset()
        return queryset

    def get_context_data(self, **kwargs):
        context = super(Yearly, self).get_context_data(**kwargs)
        context['current_year'] = self.current_year
        context['current_month'] = self.current_month
        context['year'] = self.year
        return context

【讨论】:

  • 我觉得这很奇怪,当您尝试执行context['year'] = self.kwargs['year'] 时是否有错误或其他问题?它应该可以在课堂上的任何地方访问。
  • @Ngenator:我刚刚建立了一个干净的 django 项目来仔细检查,结果证明你是正确的。我不确定是什么在我的原始代码中阻止了这种情况,但我会发现:)。感谢您的提醒
【解决方案3】:

如果你像这样传递 URL 参数:

http://<my_url>/?order_by=created

您可以使用self.request.GET 在基于类的视图中访问它(self.argsself.kwargs 中均未显示):

from django.views.generic.list import ListView

class MyClassBasedView(ListView):
    ...
    def get_queryset(self):
        order_by = self.request.GET.get('order_by') or '-created'
        qs = super().get_queryset()
        return qs.order_by(order_by)

【讨论】:

  • 谢谢!这让我很困惑......我一直在阅读暗示 HTTP 参数将在 kwargs 中的内容。
  • 你能显示MyClassBasedView的超类的get_queryset()吗?我会做qs=&lt;Object&gt;.objects.&lt;method&gt;
【解决方案4】:

我发现了这个优雅的解决方案,对于 django 1.5 或更高版本,正如here 指出的那样:

Django 的基于通用类的视图现在自动包含一个视图 上下文中的变量。此变量指向您的视图对象。

在你的views.py中:

from django.views.generic.base import TemplateView    

class Yearly(TemplateView):
    template_name = "calendars/yearly.html"
    # Not here 
    current_year = datetime.datetime.now().year
    current_month = datetime.datetime.now().month

    # dispatch is called when the class instance loads
    def dispatch(self, request, *args, **kwargs):
        self.year = kwargs.get('year', "any_default")

    # other code

    # needed to have an HttpResponse
    return super(Yearly, self).dispatch(request, *args, **kwargs)

在此question 中找到的调度解决方案。
由于 view 已经在模板上下文中传递,因此您不必担心它。在您的模板文件 yearly.html 中,可以通过以下方式访问这些视图属性:

{{ view.year }}
{{ view.current_year }}
{{ view.current_month }}

您可以保持 urlconf 原样

值得一提的是,将信息获取到模板的上下文中会覆盖 get_context_data(),因此它在某种程度上破坏了 django 的 action bean 流程。

【讨论】:

    【解决方案5】:

    不如只使用 Python 装饰器来让这个理解:

    class Yearly(TemplateView):
    
        @property
        def year(self):
           return self.kwargs['year']
    

    【讨论】:

    • 我喜欢这个。该属性是可重复使用的。
    猜你喜欢
    • 2012-12-28
    • 2020-06-30
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    相关资源
    最近更新 更多