Category:2017 films
Category:Indian filmsQ:
TypeError: must be unicode or str, not list when uploading file with Django
I have a model:
class Message(models.Model):
sender = models.ForeignKey(User)
subject = models.CharField(max_length=50)
body = models.TextField()
timestamp = models.DateTimeField(default=datetime.now)
and I have a view:
@csrf_exempt
def upload_image(request):
if request.method == 'POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponse('Image uploaded successfully')
else:
form = ImageForm()
return render_to_response('upload.html', {'form': form})
and forms.py
from django import forms
class ImageForm(forms.ModelForm):
class Meta:
model = Message
fields = ['sender','subject', 'body', 'timestamp', 'thumbnail']
On the html template I have:
{% csrf_token %}
{{ form.subject }}
{% for field in form %}
{{ field }}
{% endfor %}
My problem is that I can't upload any files with this method. On my form I only have this:
{{ field.subject }}
And I have an error like this:
TypeError: must be unicode or str, not list
A:
You have to write the form as be359ba680
Related links:
Comments