2013-10-28 114 views
0

我有問題顯示圖像上傳的圖像。以下是相關部分:Django上傳的圖片不顯示

models.py而不是完整的圖像路徑

def get_uplaod_file_name(instance,filename): #rename the uploaded file 
    return 'uploaded_files/%s_%s' % (str(time()).replace('.','_'), filenam 

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    name = models.CharField(max_length=30) 
    occupation = models.CharField(max_length=50) 
    city = models.CharField(max_length=30) 
    thumbnail = models.FileField(upload_to=get_uplaod_file_name) 

fomrs.py

class UserProfileForm(forms.ModelForm): 

    class Meta: 
     model= UserProfile 
     fields = ('name', 'occupation', 'city', 'thumbnail') 

views.py

@login_required 
def user_profile(request): 
    if request.method == 'POST': 
     form = UserProfileForm(request.POST, request.FILES, 
           instance=request.user.profile) 
     if form.is_valid(): 
      form.save() 
      message = 'Your profile is updated!' 
      return render_to_response('home.html',{'message':message}, 
             context_instance=RequestContext(request)) 

在模板中,我剛剛得到:

<img src="/static/assets/uploaded_files/" width="200" /> 

雖然圖像文件已正確上載到靜態文件夾中。所以我真的很困惑,並欣賞你的提示。

回答

2

您的模板是否包含以下內容?

{% load staticfiles %} 

您是否在settings.py中定義了靜態文件夾?

STATICFILES_DIRS = (
    'filepath/to/folder' 
) 

嘗試加載圖片這樣

<img src="{% static "/static/assets/uploaded_files/" %}" width="200" /> 
+0

其實這個問題是在定義縮略圖模板一個愚蠢的錯誤。我將其修改爲,現在顯示。欣賞你的提示科迪。 – supermario