So I have spent a number of hours trying to figure this out and there's tonnes of examples on the topic but none have them have been able to solve my problem so I'm creating another question. Basically I have a form with an ImageField and when I try and when I post with the form no image is uploaded to MEDIA_URL.
So in the models.py I keep a default because an image is not required to make a posting but when I try and create a posting with an image no image is uploaded. When I display the image on a template it reverts to the default. Am I missing something to make the image upload to MEDIA_URL? My code is below...
Models.py
class Posting(models.Model):
textbook = models.ForeignKey(Textbook)
condition = models.CharField(max_length = 200)
price = models.DecimalField(max_digits=5, decimal_places=2)
user = models.ForeignKey(User)
image = models.ImageField(upload_to='postingpics/', default="../../static/textchange/nophoto.jpg")
post_date = models.DateTimeField('date_posted')
def __str__(self):
return str(self.textbook)
def was_posted_recently(self):
return self.post_date >= timezone.now() - datetime.timedelta(days=1)
was_posted_recently.admin_order_field = 'post_date'
was_posted_recently.boolean = True
was_posted_recently.short_description = 'Posted recently'
Forms.py
class PostCreate(forms.Form):
CHOICES = (('New', 'New'), ('Like New', 'Like New'), ('Used', 'Used'), ('Usable', 'Usable'))
price = forms.DecimalField()
condition = forms.ChoiceField(choices = CHOICES)
image = forms.ImageField(required=False)
Settings.py
MEDIA_ROOT = '/home/joe/documents/exchange/Texchange/media/'
MEDIA_URL = '/media/'
Views.py
@login_required
def addposting(request, uisbn):
form = PostCreate(request.POST or None)
# Get textbook with isbn equal to usibn
ltextbook = Textbook.objects.filter(isbn = uisbn)
text = ltextbook[0]
curuser = request.user
if form.is_valid() and request.POST:
condition = request.POST.get('condition')
price = request.POST.get('price')
image = request.POST.get('image')
if image:
if (not (Posting.objects.filter(Q(user = curuser) & Q(textbook = text)))):
new = Posting(textbook = text, user = curuser, post_date = datetime.now(), condition=condition, price=price, image = image)
new.save()
return HttpResponseRedirect('/results/' + uisbn)
else:
if (not (Posting.objects.filter(Q(user = curuser) & Q(textbook = text)))):
new = Posting(textbook = text, user = curuser, post_date = datetime.now(), condition=condition, price=price)
new.save()
return HttpResponseRedirect('/results/' + uisbn)
return render_to_response(
'textchange/addposting.html',
locals(),
context_instance=RequestContext(request)
)
addposting.html - the form
<form action="{% url 'textchange:addposting' uisbn=text.isbn %}" method="POST" enctype="multipart/form-data"> {% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add a Posting"></input>
</form>
Template for viewing image
{% block content %}
<img src="{{ posting.image.url }}">
{% endblock %}
An additional question I have that I don't understand is when I go to production with this app. where are peoples pictures going to be uploaded?
Thanks.