基于Django类的视图(TemplateView)中的URL参数和逻辑

2020/12/07 04:52 · python ·  · 0评论

我不清楚在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理想情况下应将逻辑放置在哪里?在某种方法上?

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

如果您这样传递URL参数:

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

您可以使用self.request.GET(在self.args或中都未提供)在基于类的视图中访问它self.kwargs

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

我找到了这个优雅的解决方案,并且针对django 1.5或更高版本,如此处所述

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)

在这个问题中找到了调度解决方案

由于
视图已经在Template上下文中传递,因此您实际上不必担心它。在模板文件Annual.html中,可以通过以下方式简单地访问这些视图属性:

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

您可以保持urlconf不变

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

到目前为止,尽管我只使用ListView而不是TemplateView进行尝试,但我只能从get_queryset方法中访问这些url参数。我将使用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

仅仅使用Python装饰器使它变得可理解的怎么样:

class Yearly(TemplateView):

    @property
    def year(self):
       return self.kwargs['year']
本文地址:http://python.askforanswer.com/jiyudjangoleideshitutemplateviewzhongdeurlcanshuheluoji.html
文章标签: ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!