Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

pdf generation - Django view html to pdf executed twice

I have following Class to generate a pdf file where I use django-renderpdf to generate a pdf from a html template. But the view is executed twice and an error is thrown.

My class:

class WeeklyMetre(PDFView):
    template_name = 'reports/invoice/weekly_metre.html'

    allow_force_html = True
    prompt_download = True

    @property
    def download_name(self) -> str:
        invoice = Invoice.objects.get(pk=self.kwargs['pk'])
        return f"WeeklyMetre_{invoice.invoice_number}.pdf"

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        invoice = Invoice.objects.get(pk=self.kwargs.get('pk'))
        market_labor_specifications = _getWeeklyMetreData(invoice=invoice)

        # calculate reported items: reported market_labor_specifications
        # invoiced specifications which are validated in invoice-period
        # but labor_date before invoice-period

        reported_mls = MarketLaborSpecification.objects.filter(invoice_id=self.kwargs.get('pk'), market_labor__labor_date__lt=invoice.period_from) 
        .values('market_labor__labor_date', 'specification__position', 'specification__name') 
        .order_by('market_labor__labor_date', 'specification__position', 'specification__name') 
        .annotate(sum_pos=Sum('validated_quantity'))

        context.update({
            'invoice': invoice,
            'market_labor_specifications': market_labor_specifications,
            'reported_mlss': reported_mls
        })

        print('context data', datetime.datetime.now())
        return context

Between the two excutions I have following error:

[01/Feb/2021 07:16:38] "GET /reports/invoice/select/17/ HTTP/1.1" 200 1414
context data 2021-02-01 07:16:44.835695
[01/Feb/2021 07:16:45] "GET /reports/weekly/metre/17/ HTTP/1.1" 200 58063
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 60114)
Traceback (most recent call last):
  File "/usr/lib/python3.6/socketserver.py", line 654, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python3.6/socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.6/socketserver.py", line 724, in __init__
    self.handle()
  File "/home/t3tr4ktys/python-virtual-environments/BillOfQuantities/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 174, in handle
    self.handle_one_request()
  File "/home/t3tr4ktys/python-virtual-environments/BillOfQuantities/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "/usr/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer
----------------------------------------
context data 2021-02-01 07:16:47.544189
[01/Feb/2021 07:16:48] "GET /reports/weekly/metre/17/ HTTP/1.1" 200 58063

First of all I don't know why it is executed twice and on the second execution the user is no more authenticated. At the end the pdf is generated well but I cannot apply the LoginRequiredMixin. More informations will be provided if needed and thank you for any help.

question from:https://stackoverflow.com/questions/65915134/django-view-html-to-pdf-executed-twice

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like you closed tab in browser or browser stopped to accept response from server in some another way. But web server haven't stopped it's renderring and still generates response, but do not have whom to response. That's why you see:

ConnectionResetError: [Errno 104] Connection reset by peer

After that browser does retry with page reload and you see the second request in logs.

So, you need to understand why it happens. See what browser logs and Dev.Tools will show you (statuses, response and etc.).

P.S. About LoginRequiredMixin - don't know probably some internal things of browser caused with that "retry", but I believe it still should send cookies and session_id.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...