Created
July 17, 2015 06:02
-
-
Save easydevmixin/027dfcdf3ccb573c45b2 to your computer and use it in GitHub Desktop.
Easily dealing with multiple files in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
{% load i18n staticfiles %} | |
<html> | |
<!-- file: add_attachment.html --> | |
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title>{% block title %}{% endblock %}</title> | |
</head> | |
<body> | |
<form action="" method="post" enctype="multipart/form-data"> | |
{% csrf_token %} | |
<p> | |
<label for="id_parent_id">Parent ID:</label> | |
<input id="id_parent_id" type="text" maxlength="18" name="parent_id"> | |
</p> | |
<input type="file" name="myfiles" multiple> | |
<button type="submit">{% trans "Save" %}</button> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# models.py | |
import random | |
import string | |
import os | |
from django.db import models | |
from django.utils.timezone import now as timezone_now | |
def create_random_string(length=30): | |
if length <= 0: | |
length = 30 | |
symbols = string.ascii_lowercase + string.ascii_uppercase + string.digits | |
return ''.join([random.choice(symbols) for x in range(length)]) | |
def upload_to(instance, filename): | |
now = timezone_now() | |
filename_base, filename_ext = os.path.splitext(filename) | |
return 'my_uploads/{}_{}{}'.format( | |
now.strftime("%Y/%m/%d/%Y%m%d%H%M%S"), | |
create_random_string(), | |
filename_ext.lower() | |
) | |
class Attachment(models.Model): | |
parent_id = models.CharField(max_length=18) | |
file_name = models.CharField(max_length=100) | |
attachment = models.FileField(upload_to=upload_to) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# views.py | |
# -*- coding: utf-8 -*- | |
from django.shortcuts import redirect, render | |
from django.shortcuts import render_to_response | |
from .models import Attachment | |
def add_attachment(request): | |
if request.method == "POST": | |
parent_id = request.POST['parent_id'] | |
files = request.FILES.getlist('myfiles') | |
for a_file in files: | |
instance = Attachment( | |
parent_id=parent_id, | |
file_name=a_file.name, | |
attachment=a_file | |
) | |
instance.save() | |
return redirect("add_attachment_done") | |
return render(request, "add_attachment.html") | |
def add_attachment_done(request): | |
return render_to_response('add_attachment_done.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment