What if I don’t want any queryset in a Django view?

Asked

Viewed 54 times

0

There are urls that I only need to set a Session or query a restful....enfim. API What if I don’t want a queryset? Because Django seems to force me to define a?

Would have some kind of different view for that?

1 answer

1

I believe you are using Generic view (so your impression of needing a query), see all types of Generic views: https://docs.djangoproject.com/en/1.10/ref/class-based-views/

Most basic generic view:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.