Reputation: 1519
I want to save and filter user’s objects in my django app. After inputting the below codes, the imagefield is not uploading any image to my database and it’s not returning any image in my template.
Models
class Fin(models.Model):
user=models.ForeignKey(User)
title=models.CharField(max_length=250, help_text='3 bedroom flat for lease')
main_view=models.ImageField(upload_to="photos",blank=True, null=True)
side_view=models.ImageField(upload_to="photos",blank=True, null=True)
address=models.CharField(max_length=200)
city=models.CharField(max_length=200)
state=models.CharField(max_length=200)
guideline=models.TextField(max_length=1000)
def __unicode__(self):
return self.title
class FinForm(ModelForm):
class Meta:
model=Fin
fields=('title','main_view','side_view', 'address','city','state','guideline')
exclude=('user')
Views
def fincrib(request):
extra_data_context={}
#if there's nothing in the field do nothing.
if request. method=="POST":
form =FinForm(request.POST)
if form.is_valid():
data=form.cleaned_data
newfincribs=Fin(
user= request.user,
title=data['title'],
main_view=data ['main_view'],
side_view=data['side_view'],
address=data['address'],
city=data['city'],
state=data['state'],
guideline=data['guideline'])
newfincribs.save()
extra_data_context.update({'FinForm':form})
else:
form = FinForm()
extra_data_context.update({'FinForm':form})
extra_data_context.update({'Fins':Fin.objects.filter(user=request.user)})
return render_to_response('post.html',extra_data_context,context_instance=RequestContext(request))
Template
{% block content %}
<form action="." method="POST">
{% csrf_token %}
<center> {{FinForm.as_p}} </center>
<input type="submit" value="Submit"/>
</form>
{% for Fin in Fins %}
<tr>
<a href="{% url profiles_edit_profile %}"> {{Fin.user}} </a> </p> </strong>
<p>{{Fin.title}}</p>
<p><img src="{{MEDIA_URL}}/{{Fin.main_view}}"/></p>
<p> <img src="{{MEDIA_URL}}/{{Fin.side_view}}"/></p>
<p> {{Fin.address}} </p>
<p> {{Fin.city}}</p>
<p> {{Fin.state}}</p>
<p> {{Fin.guideline}}</p>
{% endfor %}
{% endblock %}
Upvotes: 0
Views: 98
Reputation: 1196
Read binding uploaded files to a form from django official docs. I think it can help.
Upvotes: 0
Reputation: 7858
You're missing a number of things on the template and view layer.
Read this: https://docs.djangoproject.com/en/1.3/topics/http/file-uploads/#basic-file-uploads
Upvotes: 2