0

I'm learning django and i'm trying to create my own custom class in a views.py file

This class i would use have to method, one for classical HTML rendering, and another for json response

my class in views.py

class myListView():
    context = {}
    def __init__(self, request):
        request = request
        context['PageTitle'] = 'Contacts'
        context['people'] = People.objects.all()

    def htmlRender(self, *args, **kwargs):
        context['children_template'] = 'people/list.html'
        return render(request,'base.html',context)

    def jsonRender(self, *args, **kwargs):
        return HttpResponse(json.dumps(self.context['people']), content_type="application/json")

my urls.py

    path('list', login_required(myListView.htmlRender()), name='list'),
    path('list/json', login_required(myListView.jsonRender()), name='list'),

Here is the error sended by debugger :

TypeError: htmlRender() missing 1 required positional argument: 'self'

I don't have any idea how to solve this, maybe i'm dreaming about using custom class in view ?

Thanks'you

2
  • What about myListView().htmlRender() ? Commented Mar 23, 2019 at 9:53
  • If i write this in urls, that is the reponse TypeError: __init__() missing 1 required positional argument: 'request' Commented Mar 23, 2019 at 10:03

2 Answers 2

1
from django.views.generic import ListView

class myListView(ListView):

Maybe you are not extending the ListView Class, try this out.

Sign up to request clarification or add additional context in comments.

Comments

0

You should first create an instance from "myListView" class then use it :

myListViewInstance = myListView(arguments)

myListViewInstance.htmlRender()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.