2011-07-21 87 views
1

我有一個小問題。我想要一個可以上傳和顯示圖像的django應用程序。目前,它可以上傳圖像,但我無法顯示該圖像。想要顯示圖像

例如,{{comment.photo}}將打印出路徑C:/Users/AQUIL/Desktop/myproject/images/P1000992.JPG。但我想在屏幕上看到這個圖像。不是路徑。如何將圖像打印到屏幕上?

以下是一些可能有所幫助的信息。

models.py

class Comment(models.Model): 
    name = models.CharField(max_length = 40) 
    datetime = models.DateTimeField(default=datetime.now) 
    photo = models.ImageField(upload_to='C:/Users/AQUIL/Desktop/myproject/media/images', blank=True, null=True) 
    note = models.TextField() 
    def __unicode__(self): 
     return unicode(self.name) 

views.py

def home(request): 
    comments = None 
    try: 
     comments = Comment.objects.order_by('-datetime') 
    except: 
     return HttpResponseNotFound() 
    return render_to_response('home.html', {'comments':comments}, context_instance=RequestContext(request)) 


def add_notes(request): 
    comments = Comment.objects.all() 
    if request.method == 'POST': 
     form = CommentForm(request.POST or None, request.FILES) 
     if form.is_valid(): 
      comments.datetime = datetime.now() 
      form.save(True) 
      return HttpResponseRedirect(reverse(home)) 
    else: 
     form = CommentForm() 
    return render_to_response('form.html', {'form':form,'comments':comments}, context_instance = RequestContext(request)) 

home.html做爲

{% extends "base.html" %} 

    {% block content %} 

    <H2>List of Comments</H2> 
    <div style="overflow:auto;padding: 10px; border:1px solid black; height:150px; width:700px;"> 

     {% for comment in comments %} 
      {{comment.photo}} <br/> 
      <b>Posted by: {{ comment.name }} Date: {{ comment.datetime.date }} Time: {{comment.datetime.time}}</b><br/> 
      <div style="font-size:125%">{{ comment.note }}</div><br/> 
     {% endfor %} 
     </div> 
    {% endblock %} 

form.html

{% extends "base.html" %}  
{% block content %} 

<h3>Add Notes</h3> 
    <form enctype="multipart/form-data" action="" method="POST"> 

{% csrf_token %} 
     <table> 
     {{form.as_table}} 
<br/> 
     </table> 

    <input type="submit" value="Save" STYLE="background-color:#E8E8E8; color:#181818 "/> 
    </form> 


{% endblock %} 
+0

請停止在標題寫標籤。 –

回答

4
{% if comment.photo %} <img src="{{ comment.photo.url }}" alt="Photo" /> {% endif %} 

見傑弗裏的評論樣本。

+0

我在渲染時遇到此錯誤:Caught ValueError:'photo'屬性沒有與之關聯的文件。那裏的照片屬性錯誤正在討論的是在{{comment.photo.url}}' – Shehzad009

+0

放入它,如果例如{%如果comment.photo%} {{comment.photo.url}} {%endif%} – JamesO

+0

你有沒有沒有上傳照片的一些評論? – agf

0

而是然後{{comment.photo}}

使用{{comment.photo.url}}

看到了如何將正確的圖像上傳docs

4

的ImageField的上傳參數必須是本地路徑,所以更換:

photo = models.ImageField(upload_to='C:/Users/AQUIL/Desktop/myproject/media/images', blank=True, null=True) 

由:

photo = models.ImageField(upload_to='images', blank=True, null=True) 

然後設置MEDIA_ROOT在settings.py爲:

MEDIA_ROOT = 'C:/Users/AQUIL/Desktop/myproject/media/' 

最後,你的圖片'myImage.png'將可以在:

C:/Users/AQUIL/Desktop/myproject/media/images/myImage.png 

而這個標籤應該加載圖像:

<img src="/media/images/myImage.png" alt=""/> 

取決於你MEDIA_URL在settings.py應該是:

MEDIA_URL = '/media/' 
相關問題