我在显示通过表单上传的图像时遇到问题。以下是相关部分:
模型.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" />
虽然图像文件已正确上传到静态文件夹。所以我真的很困惑,很感激你的提示。