Django 1.7 - ImageField,我的图片没有上传到MEDIA_URL

时间:2015-09-17 19:40:20

标签: python django imagefield

所以我花了好几个小时试图解决这个问题以及关于这个主题的大量例子,但没有一个能够解决我的问题,所以我要创建另一个问题。基本上我有一个带有ImageField的表单,当我尝试时,当我使用表单发布时,没有图像上传到MEDIA_URL。

所以在models.py中我保留了默认值,因为不需要图像来发布,但是当我尝试创建带图像的帖子时,没有上传图像。当我在模板上显示图像时,它将恢复为默认值。我错过了将图片上传到MEDIA_URL的内容吗?我的代码如下......

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 - 表单

<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>

查看图片的模板

{% block content %}
    <img src="{{ posting.image.url }}">
{% endblock %}

我有一个我不明白的另一个问题是,当我使用此应用程序进行制作时。人民图片将在何处上传?

感谢。

1 个答案:

答案 0 :(得分:1)

您没有将request.FILES参数传递给表单。

你需要这样做:

form = PostCreate(request.POST or None, request.FILES or None)

有关详细信息,请参阅:https://docs.djangoproject.com/en/1.7/topics/http/file-uploads/

文件将上传到MEDIA_ROOT

指定的目录