How to receive a PDF file in Django and save to a directory?

Asked

Viewed 677 times

1

I get an html file using type="file".

<form action="/docs/enviado/" method="post">{% csrf_token %}
  <input type="file" accept="pdf" name = "projeto"/><br /><br />
  <input type="submit" value="enviar" />
</form>

But I can’t save using python, I tried so but it didn’t work:

projeto = request.POST.get('projeto')
projeto.save

And so:

projeto = request.FILES["projeto"]
projeto.save

I just need to receive the pdf file and save in the directory that is already.

1 answer

0


In your form

<form action="/docs/enviado/" method="post" enctype="multipart/form-data">

In your model field:

meuarquivo = models.FileField(upload_to='meusarquivos/')

In your view

form = MeuForm(request.POST, request.FILES)
  • So no way to do it without using models?

  • You can do without using the model yes. You just have to see what you will do with the file and how you will save in the directory.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.