gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:29 24 4
gpt4 key购买 nike

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

因此,在 models.py 中,我保留默认值,因为发布帖子不需要图像,但当我尝试使用图像创建帖子时,不会上传图像。当我在模板上显示图像时,它会恢复为默认值。我是否缺少将图像上传到 MEDIA_URL 的某些内容?我的代码如下...

模型.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'

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

设置.py

MEDIA_ROOT = '/home/joe/documents/exchange/Texchange/media/'

MEDIA_URL = '/media/'

View .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指定的目录

关于python - Django 1.7 - ImageField,我的图像没有上传到 MEDIA_URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32638677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com