2013-04-17 87 views
-1

我已經爲創建用戶配置文件創建了一個管理員表單。Django:image does not display

有沒有爲圖像上傳到服務器

avatar = models.ImageField("Profile Pic", upload_to="images/", blank=True, null=True) 

它工作正常,可以將圖像上傳到服務器的域。現在我想顯示這個圖像,因爲我已經實現了一些代碼。但它不起作用。

代碼views.py文件

user_profile = request.user.get_profile() 
return render_to_response('view_profile.html',{'profile':user_profile },context_instance=RequestContext(request)) 

代碼的html文件

<img src="{{ MEDIA_URL }}{{ profile.avatar}}" alt="No Profile picture" height="420" width="420"> 

我打印{{ MEDIA_URL }}{{ profile.avatar}}那麼它打印出來放在爲"/home/calcey/Documents/login/images/Nelumno_nucifera_open_flower_-_botanic_garden_adelaide2.jpg"。它打印正確的圖像url,但不顯示圖像。

這是什麼原因?

+0

檢查您的設置MEDIA_ROOT和MEDIA_URL。你可能已經交換了他們的值。閱讀文檔中的更多信息:https://docs.djangoproject.com/en/dev/ref/settings/#media-root – Omie

+0

好的。感謝您的幫助:) – Lahiruzz

+0

使用'{{profile.avatar.url}}'而不是'{{MEDIA_URL}} {{profile.avatar}}' – imkost

回答

0

在這裏我忘了導入(r'^images/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),到urls.py文件。現在它工作正常