我有一个问题,这是我的代码
上传图像成功后,它将返回所有数据( item = Item.objects.all()
)并显示在模板上
如果我只希望用户上传的图片显示在模板上(而不是数据库中的所有图像),该怎么办?
请引导我!
非常感谢你.
I have a question,this is my code
When uploade image success,It will return all data (item=Item.objects.all()
) and show on the template
What if I only want the picture which is just uploaded by user to show on template(not all images in database),how can I do this?
Please guide me !
Thank you very much.
views.py
def background(request):
if request.method=="POST":
img = ItemForm(request.POST, request.FILES)
if img.is_valid():
img.save()
return HttpResponseRedirect(reverse('imageupload:background'))
img=ItemForm()
item=Item.objects.all()
return render(request,'uploader/background.html',{'form':img,'item':item,})
models.py
models.py
from sorl.thumbnail import ImageField
class Item(models.Model):
image = ImageField(upload_to='thumb/')
class ItemForm(forms.ModelForm):
class Meta:
model = Item
模板:
<form action="#" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form}} <input type="submit" value="Upload" />
</form>
{% for i in item%}
{{i.image}}
{% thumbnail i.image "1024" crop="center" format="PNG" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
{% endfor %}