1def upload_pic(request):
2 if request.method == 'POST':
3 form = ImageUploadForm(request.POST, request.FILES)
4 if form.is_valid():
5 m = ExampleModel.objects.get(pk=course_id)
6 m.model_pic = form.cleaned_data['image']
7 m.save()
8 return HttpResponse('image upload success')
9 return HttpResponseForbidden('allowed only via POST')
1<form action="{% url upload_pic %}" method="post" enctype="multipart/form-data">{% csrf_token %}
2 <p>
3 <input id="id_image" type="file" class="" name="image">
4 </p>
5 <input type="submit" value="Submit" />
6</form>
1class ExampleModel(models.Model):
2 model_pic = models.ImageField(upload_to = 'pic_folder/', default = 'pic_folder/None/no-img.jpg')
1class ImageUploadForm(forms.Form):
2 """Image upload form."""
3 image = forms.ImageField()