Would you return an argument where you should return two in django's views?


Good afternoon dear stackoverflow friends I have this doubt, I'm trying to print pdfs from django with the xhtml2pdf library for my projects in django2.0 , I follow a tutorial that I find interesting that the source is:


My error on the screen is as follows:

__ init __ () takes 1 positional argument but 2 were given

I follow the tutorial from up there to the letter, maybe it's the Django version but I'll put my code here below


from django.http import HttpResponse
from django.views.generic import View
from .pdf_generator import render_to_pdf
class GeneratePdf(View):
    def get(self, request, *args, **kwargs):
        data = {}
        pdf = render_to_pdf('pdf/invoice.html', data)
        return HttpResponse(pdf, content_type='application/pdf')


from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template

from xhtml2pdf import pisa

def render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html  = template.render(context_dict)
    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
     return HttpResponse(result.getvalue(), content_type='application/pdf')
    return None


from django.urls import path
from . import views

urlpatterns = [
    path('pdf/', views.GeneratePdf),


{% extends "../base.html" %}

{% block content_style %}
body {
    font-family: "Helvetica", "sans-serif";
    color: #333333;
{% endblock content_style %}

{% block content %}
    <h1>Hello world!</h1>
    <h2>Welcome to the pdf!</h2>
{% endblock content %}

just in case I leave you something called the traceback :


Request Method: GET
Request URL:

Django Version: 2.0
Python Version: 3.6.5
Installed Applications:
Installed Middleware:


File "/usr/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/usr/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/usr/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

Exception Type: TypeError at /cv/pdf/
Exception Value: __init__() takes 1 positional argument but 2 were given

Excuse me so much code is that I want to be as explicit as possible because I really do not know what to do I have tried several libraries and it is always this error which persists.

asked by Tysaic 02.06.2018 в 05:23

1 answer


To use Class Based Views you have to call your as_view() method. Therefore, you have to modify your URL in this way:

from django.urls import path
from . import views

urlpatterns = [
    path('pdf/', views.GeneratePdf.as_view()),
answered by 02.06.2018 / 17:20