因此,我的目标是让用户点击发票的链接,然后打开一个窗口,其中显示该发票的PDF。目前,我收到以下错误:
NoReverseMatch at /laptops/invoices/
Reverse for 'pdfview' with keyword arguments '{'invoice': 'uploads/wordpress-pdf-invoice-plugin-sample.pdf'}' not found. 1 pattern(s) tried: ['laptops\\/invoices\\/(?P<invoice>[^/]+)\\/$']
糟糕的编程习惯,我知道。我只是想在我清理它之前让它工作,但我不知道我做错了什么。
urls.py
urlpatterns = [
path('',views.laptop_list, name ="list"),
path('add/',views.laptop_add,name="add"),
path('invoices/', views.invoice_list,name="invoices"),
path('invoices/<str:invoice>/', views.pdf_view, name ="pdfview")
]
views.py
def pdf_view(request, invoice):
invoicename=Laptop.objects.get(invoicename=invoice)
pdfpath = settings.MEDIA_ROOT
with open(pdfpath+'/'+invoicename, encoding="latin-1") as pdf:
response = HttpResponse(pdf.read(), content_type='application/pdf')
return response
laptop_invoices.html
<h2>View invoices</h2>
{%for i in invoices%}
<ul>
<li>
<a href = "{%url 'laptops:pdfview' invoice=i.invoice%}"> {{i.invoice}} </a>
</li>
</ul>
{%endfor%}
答案 0 :(得分:0)
我明白了。 invoicename=Laptop.objects.get(invoicename=invoice)
正在返回QuerySet
而不是string
,因此PDF的路径永远不会起作用。谢谢你的帮助。