繁体   English   中英

Django 1.7-ImageField,我的图像未上传到MEDIA_URL

[英]Django 1.7 - ImageField, my image is not being uploaded to MEDIA_URL

因此,我花了几个小时试图弄清楚这一点,并且有很多关于该主题的示例,但是这些示例都无法解决我的问题,因此我提出了另一个问题。 基本上,我有一个带有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 %}

我还有一个我不明白的问题,就是当我使用此应用进行生产时。 人们的图片要上传到哪里?

谢谢。

您没有将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指定的目录

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM