0

我在显示通过表单上传的图像时遇到问题。以下是相关部分:

模型.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)

表格.py

class UserProfileForm(forms.ModelForm):

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

视图.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" />

虽然图像文件已正确上传到静态文件夹。所以我真的很困惑,很感激你的提示。

4

1 回答 1

2

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

{% load staticfiles %}

您是否在 settings.py 中定义了静态文件夹?

STATICFILES_DIRS = (
    'filepath/to/folder'
)

尝试加载这样的图片

<img src="{% static "/static/assets/uploaded_files/" %}" width="200" />
于 2013-10-28T21:59:12.150 回答