I have a simple auth app in Django and now I want the authentication users to can upload easy multiple files from page on website.
i have create a multiupload Django form and views and html form i am not sure if my code is perfect first time play with auth but not work anyway.
in the html page don't show me the form for upload show only the submit button and do nothing that.
model.py
class MyModel(models.Model):
user = models.ForeignKey(User, unique=True)
upload = models.ImageField(upload_to='upload')
views.py
@login_required
def upload_images(request):
uploadimages = UploadImagesForm(request.POST or None, request.FILES or None)
if uploadimages.is_valid():
# Get the images that have been browsed
if request.FILES.get('multipleimages', None):
images = request.FILES.getlist('multipleimages')
for image in images:
MyModel.objects.create(user=request.user.id, images=image)
else:
pass
else:
raise Http404
return render(request, 'home.html', {'UploadImagesForm':UploadImagesForm })
forms.py
class UploadImagesForm(forms.Form):
multipleimages = forms.ImageField(label="Upload Multiple Images:",widget=forms.ClearableFileInput(attrs={'multiple': True}), required=False)
html
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="row">
<div class="jumbotron">
<h1>Hello</h1>
<p><form class="" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ UploadImagesForm }}
<input type="submit" name="" value="Submit">
</form></p>
<img src="static/css/me.jpg" width="777px" height="555px";alt="some image"/>
</div>
</div>
</div>
{% endblock %}
答案 0 :(得分:0)
图片无效时,您正在筹集404页面。这不是非常友好的用户体验!
更改为:
@login_required
def upload_images(request):
uploadimages = UploadImagesForm(request.POST or None, request.FILES or None)
if uploadimages.is_valid():
# Get the images that have been browsed
if request.FILES.get('multipleimages', None) is not None:
images = request.FILES.getlist('multipleimages')
for image in images:
MyModel.objects.create(user=request.user, upload=image)
return render(request, 'home.html', {'UploadImagesForm':UploadImagesForm })