2013-10-28 116 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