简体   繁体   中英

How to make a file uploader (.jpg, .mp3, .pdf etc) from local directory for a user in django

Need an idea about how we can get that file from the user that he has selected from the local directory and send it to database.

i would suggest to use the django FileField, this wont store the file in the database directly (would also cause problems with large files eg videos), but rather uploads the file to the server and stores the path in the database.


EDIT:

More infos about the FileField can be found here . You will need to create a ModelField in your model and define the path, where the file should be stored via upload_to .

For the upload process itself you have to create a view (easiest way would be a CreateView (more infos here ). This would enable a user to access the page (view) and create a model instance with the desired file.


EDIT2: You'll need a simple HTML-Form for this, the CreateView will provide it for you. In the template you can simply use:

<form method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">
</form>

which will lead to

<form method="post">
    <input type="hidden" value="***">
    <input type="file">
    <input type="submit" value="Submit">
</form>

I would suggest to you to have a look at Django / HTML basics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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