简体   繁体   English

将动态下载路径设置为django

[英]Set dynamic download path to django

How i can set a dynamic download path to django by change this part: 我如何通过更改此部分来设置django的动态下载路径:

(settings.MEDIA_ROOT, 'folder/path/file.pdf') (settings.MEDIA_ROOT,“文件夹/路径/文件.pdf”)

    with open(os.path.join(settings.MEDIA_ROOT, 'folder/path/file.pdf'), 'rb') as fh:
    response = HttpResponse(fh.read(), content_type="application/pdf")
    response['Content-Disposition'] = 'filename=invoice.pdf'
    return response

to something like this: 像这样:

(settings.MEDIA_ROOT, FUNCTION_RETURN_THE_PATH) (settings.MEDIA_ROOT,FUNCTION_RETURN_THE_PATH)

with open(os.path.join(settings.MEDIA_ROOT, FUNCTION_RETURN_THE_PATH), 'rb') as fh:
    response = HttpResponse(fh.read(), content_type="application/pdf")
    response['Content-Disposition'] = 'filename=invoice.pdf'
    return response

If a use function i get many errors, because it expects a string variable in this place and not a function. 如果使用函数,我会遇到很多错误,因为它期望在该位置使用字符串变量而不是函数。

in my html there is a for that show all the model to user 在我的html中,有一个用于向用户显示所有模型

{% for printforme in print_user_charges %}
            <tr>
              <td>{{ printforme.invoice_number }}</td>
              <td>{{ printforme.price }} €</td>
              <td>{{  printforme.reason }}</td>
              <td>{{ printforme.customer }} {{ printforme.customer.surname }}</td>
              <td>{{ printforme.charge_date }}</td>
              <td>  <a href="{% url 'download_pdf' printforme.upload_pdf.id %}"> Download </a></td>

the download link drives into a view: 下载链接进入视图:

def DownloadPdf(request, charge_id):

with open(os.path.join(settings.MEDIA_ROOT, get_file_path), 'rb') as fh:
    response = HttpResponse(fh.read(), content_type="application/pdf")
    response['Content-Disposition'] = 'filename=invoice.pdf'
    return response

I store pdf, base to the user id like this: 我存储pdf,基于这样的用户ID:

def invoice_path_base_to_usersid(instance, filename):
return 'user_{0}/{1}'.format(instance.customer.id, filename)

So i store pdfs to media/user_{id}/filename.pdf 所以我将pdfs存储到media / user_ {id} /filename.pdf

I want user to downlaod a specific pdf file when he click on the currect item. 我希望用户单击当前项目时下载特定的pdf文件。 But i dont know how to use the user id and put it inside the open to download the currect one pdf that user want. 但是我不知道如何使用用户ID并将其放在公开位置以下载用户想要的当前一个pdf。

您尚未调用该函数。

with open(os.path.join(settings.MEDIA_ROOT, get_file_path()), 'rb') as fh:

Finally i found a solution, i post it here to help anyone that struggles with that things. 终于,我找到了一个解决方案,在这里将其发布,以帮助遇到此类问题的任何人。

My main problem it was that i was try to get an instance and i do not pass it into any variable before i try to load the file. 我的主要问题是我尝试获取实例,并且在尝试加载文件之前没有将其传递给任何变量。

I change the open method to, i found the last one more clear. 我将打开方法更改为,我发现最后一个更清晰了。

def DownloadPdf(request, charges_id):

try:
    invoice = Charge.objects.get(pk=charges_id)
except Charge.DoesNotExist:
    raise Http404

response = HttpResponse(File(invoice.upload_pdf), content_type="application/pdf")
response['Content-Disposition'] = 'attachment; filename="mypdf.pdf"'
return response

So the answer is, if you want a dynamicaly download path in django you have to query your model and put the output in a variable. 因此,答案是,如果要在Django中动态下载路径,则必须查询模型并将输出放入变量中。

After that, by calling this variable, in that way: 之后,通过这种方式调用此变量:

variable.field_of_model variable.field_of_model

you can refer to any field of that record and use it as you want 您可以引用该记录的任何字段并根据需要使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM