2014-12-08 50 views
0

我有一個問題,這是我的代碼
當uploade形象的成功,這將返回所有數據(item=Item.objects.all()),並顯示在模板
如果我只想要的圖片是隻是由用戶上傳以顯示模板(不是數據庫中的所有圖像),我該怎麼做?
請指導我!
非常感謝。Django的上傳圖片並顯示圖像

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

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

回答

0

你可以有ID那裏當保存。只需在會話中設置ID。所以,當redirecting,你可以擁有這photo & user也喜歡... ID

photoId = request.session['photoId'] 
userId = request.session['_auth_user_id'] 
item=Item.objects.get(pk=photoId) #you can also use filter by userId 

代碼:

if img.is_valid(): 
    img.save() 
    request.session['photoId'] = img.id 
    return HttpResponseRedirect(reverse('imageupload:background')) 
0

如果添加了可用於場會更好跟蹤您最新上傳的圖片。

class Item(models.Model): 
    image = ImageField(upload_to='thumb/') 
    upload_time = models.DateTimeField(auto_now=False, auto_now_add=True) 

在這裏,我添加了一個名爲upload_time一個更費爾德與參數auto_now_add=True.

DateField.auto_now_add 

Automatically set the field to now when the object is first created. 
Useful for creation of timestamps. Note that the current date is always used; 
it’s not just a default value that you can override. 

之後,你可以從Item模型中使用latest方法得到的最新對象。

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.latest('upload_time') 
    return render(request,'uploader/background.html',{'form':img,'item':item,}) 

關於最新:

latest(field_name=None) 
Returns the latest object in the table, by date, using the field_name provided as the date field. 

所以你會得到那麼單一的對象就不會被要求重複它在你的模板。

{{item.image}} 
{% thumbnail item.image "1024" crop="center" format="PNG" as im %} 
    <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}"> 
{% endthumbnail %} 
0

您不必使用會話並重定向或添加任何額外的字段。 ModelForm save() method返回剛剛保存的對象。試試這個:

# views.py 
def background(request): 
    form = ItemForm(request.POST or None, request.FILES or None) 
    item = None 
    if request.method=="POST" and form.is_valid():  
     item = form.save() 
    return render(request,'uploader/background.html',{'form': form,'item': item,}) 


# template 
<form action="#" method="post" enctype="multipart/form-data"> 
{% csrf_token %} 
    {{form}} <input type="submit" value="Upload" /> 
</form> 

{% if item %} 
    {{ item.image }} 
     {% thumbnail item.image "1024" crop="center" format="PNG" as im %} 
      <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}"> 
     {% endthumbnail %} 
{% endif %} 
0

我做了一些稍微不同的事情。我的視圖每次動態呈現圖形圖像(圖像),因此它不在數據庫中。但是爲了從我的模型中嵌入一個圖形的實例,這是我做的:

首先,我提出了一個視圖,它從我的數據呈現圖形並創建了一個到它的url(使用它的id進行選擇) :

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

沒有必要顯示我的看法,但如果我去了那個網址,我的視圖將被調用,它會呈現圖形和只有圖形。與大多數網站類似,如果您點擊圖片,您只會看到只顯示圖片的網頁。

現在有了這個網址的工作:

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

這種觀點帶來了這個壞小子的模板:

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

的URL部分抓住我的graphImage網址,並用它輸入graph.id 。然後我把它命名爲the_graph以保持簡單。

然後我使用圖像標籤作爲src作爲_graph。不要擔心它只是顯示網址,如果它不適用於調試。

因此,我所做的是在一個網頁上繪製圖形,並將其用作圖像標記的來源。這裏有一百萬種方法來做這種事情,但這是最明顯的,我的技能有限,只知道html和django。