gpt4 book ai didi

Django查看html到pdf执行两次

转载 作者:行者123 更新时间:2023-12-03 23:00:46 25 4
gpt4 key购买 nike

我有以下类来生成我使用的 pdf 文件 django-renderpdf从 html 模板生成 pdf。但是 View 执行了两次并抛出错误。
我的课:

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
在两次执行之间我有以下错误:
[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
首先,我不知道为什么它会被执行两次,而在第二次执行时,用户不再经过身份验证。最后,pdf 生成得很好,但我无法应用 LoginRequiredMixin。如果需要,将提供更多信息,并感谢您的帮助。

最佳答案

我相信我经历过这样的事情。
问题是当你调用你的端点时,你在 URL 中调用它,最后没有 '/' 导致 DRF 返回 301
将您重定向到相同的路径,但末尾带有“/”。问题在于,在重定向之后,您会丢失 header 和 cookie,从而变得未经身份验证。
这也解释了为什么您可以看到 2 个电话。
所以基本上如果你有这样的电话:

api/viewset/dosomethngs
到:
api/viewset/dosomethngs/

关于Django查看html到pdf执行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65915134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com