What if I do not want any queryset in a Django view?

0

There are urls that I just need to set a session or consult a restful API ... anyway. What if I do not want a queryset? Why does django seem to force me to define one?

Would you have some kind of different view for this?

    
asked by anonymous 16.12.2016 / 17:40

1 answer

1

I believe you are using generic view (so your impression of needing a query), see all types of generic views: link

Generic view most basic:

from django.http import HttpResponse
from django.views import View

class MyView(View):

    def get(self, request, *args, **kwargs):
        return HttpResponse('Hello, World!')

If you need to render template:

from django.views.generic.base import TemplateView

from articles.models import Article

class HomePageView(TemplateView):

    template_name = "home.html"

    def get_context_data(self, **kwargs):
        context = super(HomePageView, self).get_context_data(**kwargs)
        context['latest_articles'] = Article.objects.all()[:5]
        return context
    
18.12.2016 / 07:04