django上传图片并显示该图片

django上传图片并显示该图片

问题描述:

我有一个问题,这是我的代码
上传图像成功后,它将返回所有数据( 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 %}    

我做了一些稍有不同的事情.我的视图每次都动态地绘制一个图形(图像),因此它不在数据库中.但是要在我的模型中嵌入图的单个实例,这就是我所做的:

I did something slightly different. My view renders a graph figure (an image) dynamically each time so it is not in the database. But to embed a single instance of a graph from my models this is what I did:

首先,我创建了一个视图,该视图从数据中渲染图形,并创建了一个指向该图形的网址(通过使用其ID进行选择):

First, I made a view that renders the graph from my data and created a url that goes to it (selected by using it's id):

    url(r'^graphs/graphImage/(?P<graph_id>\d+?)/$', 'hydro.views.render_graph', name='graphImage'),

不需要显示我的视图,但是如果我转到该URL,则将调用我的视图,它将显示图形和仅图形.与大多数网站类似,如果您单击图片,您只会得到一个仅显示图片的网页.

no need to show my view, but if i went to that url my view would be called and it would render the graph and only the graph. Similar to most sites where if you click on the picture you just get a webpage showing only the picture.

现在使用此网址:

    url(r'^graphs/(?P<graph_id>\d+?)/$', 'hydro.views.single_graph', name='graph_detail'),

此视图显示了这个坏男孩的模板:

this view brings up a template with this bad boy:

   {% url 'graphImage' graph.id as the_graph %}
        <img src="{{the_graph}}" alt="{{the_graph}}"/>

URL部分获取我的graphImage URL并输入它的graph.id.然后我将其命名为the_graph,以使其保持简单.

the url portion grabs my graphImage url and inputs the graph.id with it. And then I name it the_graph to keep it simple.

然后我将图像标签作为sgraph作为_graph.不用担心alt,如果调试不起作用,它只会显示url.

then i make an image tag as the src as the _graph. Don't worry about alt it just shows the url if it doesnt work for debugging.

所以我要做的是在一个网页上绘制图形,然后将其用作图像标签的来源.有上百万种方法可以做这种事情,但这是我最有限的技能,并且只了解html和django是最明显的.

So what I did was render a graph on one webpage and just use that as a source for an image tag. Theres a million ways to do this sort of thing, but this was the most obvious with my limited skills and just knowing about html and django.