Reputation: 990
form.html
{% for fields in form %}
<div class="control-group">
<label class="control-label" for="{{ field.id_for_label }}">
{{fields.label}}</label>
<div class="controls">
{{fields}}
{% if fields.label == 'Photo' %}
<br>
<p></p>
<button type="button" class="btn btn-primary success"
id="add_new_file">+ Add Another File</button>
{% endif %}
</div>
</div>
<script>
$("#add_new_file").click(function(){
$("p").append('<input type="file"><br>');
});
</script>
For every click in + Add Another File button, another input file option added. Here i use model form to insert into the database.The first one is adder into the db, but how can i add multiple photo or other types files at the same time.
views.py
if request.method == 'POST':
form_values = Registration_Form(request.POST, request.FILES)
multiple_files = request.POST
print(multiple_files)
for file in multiple_files:
print(file)
if form_values.is_valid():
data = form_values.save(commit=False)
password = data.password.encode('utf-8')
password = hashlib.sha256(password).hexdigest()
data.password = password
activation_code = str(random.randrange(0, 999999))
data.activation_code = activation_code
data.save()
Upvotes: 0
Views: 727
Reputation: 88569
Assuming that you need to save the files blindly to storage ( project directory) and there are no model relationships.
def my_view(request):
if request.method == 'POST':
for data in request.FILES.values():
with open(data.name, 'wb') as file:
file.write(data.read())
# do other stuff
This will write/create new files to your project directory from `request.FILES
Upvotes: 1