标签归档:django-class-based-views

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

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

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

It is unclear to me how it is best to access URL-parameters in class-based-views in Django 1.5.

Consider the following:

View:

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'
    ),
)

I want to access the year parameter in my view, so I can do logic like:

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
        })

How would one best access the url parameter in CBVs like the above that is subclassed of TemplateView and where should one ideally place the logic like this, eg. in a method?


回答 0

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

To access the url parameters in class based views, use self.args or self.kwargs so you would access it by doing self.kwargs['year']


回答 1

如果您传递这样的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)

In case you pass URL parameter like this:

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

You can access it in class based view by using self.request.GET (its not presented in self.args nor in 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)

回答 2

我找到了这个优雅的解决方案,并且针对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流。

I found this elegant solution, and for django 1.5 or higher, as pointed out here:

Django’s generic class based views now automatically include a view variable in the context. This variable points at your view object.

In your 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)

The dispatch solution found in this question.
As the view is already passed within Template context, you don’t really need to worry about it. In your template file yearly.html, it is possible to access those view attributes simply by:

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

You can keep your urlconf as it is.

It’s worth mentioning that getting information into your template’s context overwrites the get_context_data(), so it is somehow breaking the django’s action bean flow.


回答 3

到目前为止,尽管我只使用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

So far I’ve only been able to access these url parameters from within the get_queryset method, although I’ve only tried it with a ListView not a TemplateView. I’ll use the url param to create an attribute on the object instance, then use that attribute in get_context_data to populate the context:

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

回答 4

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

class Yearly(TemplateView):

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

How about just use Python decorators to make this intelligible:

class Yearly(TemplateView):

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

不可JSON序列化

问题:不可JSON序列化

我有以下代码序列化查询集;

def render_to_response(self, context, **response_kwargs):

    return HttpResponse(json.simplejson.dumps(list(self.get_queryset())),
                        mimetype="application/json")

以下是我的 get_querset()

[{'product': <Product: hederello ()>, u'_id': u'9802', u'_source': {u'code': u'23981', u'facilities': [{u'facility': {u'name': {u'fr': u'G\xe9n\xe9ral', u'en': u'General'}, u'value': {u'fr': [u'bar', u'r\xe9ception ouverte 24h/24', u'chambres non-fumeurs', u'chambres familiales',.........]}]

我需要序列化。但是它说无法序列化<Product: hederello ()>。因为列表由Django对象和字典组成。有任何想法吗 ?

I have the following code for serializing the queryset;

def render_to_response(self, context, **response_kwargs):

    return HttpResponse(json.simplejson.dumps(list(self.get_queryset())),
                        mimetype="application/json")

And following is my get_querset()

[{'product': <Product: hederello ()>, u'_id': u'9802', u'_source': {u'code': u'23981', u'facilities': [{u'facility': {u'name': {u'fr': u'G\xe9n\xe9ral', u'en': u'General'}, u'value': {u'fr': [u'bar', u'r\xe9ception ouverte 24h/24', u'chambres non-fumeurs', u'chambres familiales',.........]}]

Which I need to serialize. But it says not able to serialize the <Product: hederello ()>. Because list composed of both django objects and dicts. Any ideas ?


回答 0

simplejson并且json不能很好地与Django对象配合使用。

Django的内置序列化器只能序列化由django对象填充的查询集:

data = serializers.serialize('json', self.get_queryset())
return HttpResponse(data, content_type="application/json")

就您而言,self.get_queryset()其中包含django对象和dict的混合。

一种选择是摆脱中的模型实例,self.get_queryset()并使用dict将其替换为model_to_dict

from django.forms.models import model_to_dict

data = self.get_queryset()

for item in data:
   item['product'] = model_to_dict(item['product'])

return HttpResponse(json.simplejson.dumps(data), mimetype="application/json")

希望能有所帮助。

simplejson and json don’t work with django objects well.

Django’s built-in serializers can only serialize querysets filled with django objects:

data = serializers.serialize('json', self.get_queryset())
return HttpResponse(data, content_type="application/json")

In your case, self.get_queryset() contains a mix of django objects and dicts inside.

One option is to get rid of model instances in the self.get_queryset() and replace them with dicts using model_to_dict:

from django.forms.models import model_to_dict

data = self.get_queryset()

for item in data:
   item['product'] = model_to_dict(item['product'])

return HttpResponse(json.simplejson.dumps(data), mimetype="application/json")

Hope that helps.


回答 1

最简单的方法是使用JsonResponse

对于查询集,您应传递该查询集的的列表values,如下所示:

from django.http import JsonResponse

queryset = YourModel.objects.filter(some__filter="some value").values()
return JsonResponse({"models_to_return": list(queryset)})

The easiest way is to use a JsonResponse.

For a queryset, you should pass a list of the the values for that queryset, like so:

from django.http import JsonResponse

queryset = YourModel.objects.filter(some__filter="some value").values()
return JsonResponse({"models_to_return": list(queryset)})

回答 2

我发现可以使用“ .values”方法相当简单地完成此操作,该方法还提供了命名字段:

result_list = list(my_queryset.values('first_named_field', 'second_named_field'))
return HttpResponse(json.dumps(result_list))

必须使用“列表”来获取可迭代的数据,因为“值查询集”类型仅当作为可迭代的拾取时才是字典。

文档:https : //docs.djangoproject.com/en/1.7/ref/models/querysets/#values

I found that this can be done rather simple using the “.values” method, which also gives named fields:

result_list = list(my_queryset.values('first_named_field', 'second_named_field'))
return HttpResponse(json.dumps(result_list))

“list” must be used to get data as iterable, since the “value queryset” type is only a dict if picked up as an iterable.

Documentation: https://docs.djangoproject.com/en/1.7/ref/models/querysets/#values


回答 3

从1.9版本开始,更轻松和官方的获取json的方式

from django.http import JsonResponse
from django.forms.models import model_to_dict


return JsonResponse(  model_to_dict(modelinstance) )

From version 1.9 Easier and official way of getting json

from django.http import JsonResponse
from django.forms.models import model_to_dict


return JsonResponse(  model_to_dict(modelinstance) )

回答 4

我们的js程序员要求我向她返回确切的JSON格式数据,而不是json编码的字符串。

下面是解决方案(这将返回一个可以在浏览器中直接使用/查看的对象)

import json
from xxx.models import alert
from django.core import serializers

def test(request):
    alert_list = alert.objects.all()

    tmpJson = serializers.serialize("json",alert_list)
    tmpObj = json.loads(tmpJson)

    return HttpResponse(json.dumps(tmpObj))

Our js-programmer asked me to return the exact JSON format data instead of a json-encoded string to her.

Below is the solution.(This will return an object that can be used/viewed straightly in the browser)

import json
from xxx.models import alert
from django.core import serializers

def test(request):
    alert_list = alert.objects.all()

    tmpJson = serializers.serialize("json",alert_list)
    tmpObj = json.loads(tmpJson)

    return HttpResponse(json.dumps(tmpObj))

回答 5

首先,我在模型中添加了to_dict方法;

def to_dict(self):
    return {"name": self.woo, "title": self.foo}

然后我有这个;

class DjangoJSONEncoder(JSONEncoder):

    def default(self, obj):
        if isinstance(obj, models.Model):
            return obj.to_dict()
        return JSONEncoder.default(self, obj)


dumps = curry(dumps, cls=DjangoJSONEncoder)

最后使用此类来序列化我的查询集。

def render_to_response(self, context, **response_kwargs):
    return HttpResponse(dumps(self.get_queryset()))

这个效果很好

First I added a to_dict method to my model ;

def to_dict(self):
    return {"name": self.woo, "title": self.foo}

Then I have this;

class DjangoJSONEncoder(JSONEncoder):

    def default(self, obj):
        if isinstance(obj, models.Model):
            return obj.to_dict()
        return JSONEncoder.default(self, obj)


dumps = curry(dumps, cls=DjangoJSONEncoder)

and at last use this class to serialize my queryset.

def render_to_response(self, context, **response_kwargs):
    return HttpResponse(dumps(self.get_queryset()))

This works quite well