简体   繁体   中英

Django generic views extra context

I am trying to use the generic DetailView and add some context there to use in a template.

My view looks like

class SpecsView(DetailView):
    model = Specification

    def get_context_data(self, **kwargs):
        context = super(SpecsView, self).get_context_data(**kwargs)
        pretty_json = "So pretty so pretty"
        context['pretty_json'] = pretty_json
        context['hello'] = "Hellow hellow"
        return context

As you can see I added two fields before returning the context, as per the tutorial / doc in Django.

In my template, I have (fraction)

    <p> Specs should come here.</p>
    {{ specification.pretty_json }}
    {{ specification.hello }}
    <p> No further.</p>

Everything else works, ie I can use and display any other fields of my model Specification.

But nothing displays when I use the extended context.

HTML result -->

Specs should come here.

No further.

Hopefully I am missing something obvious. I am using Python 3.4, django 1.8.5

Thanks!

You are adding pretty_json and hello directly in context, so to call them use:

{{ pretty_json }}
{{ hello }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM