首先,如果有一个完整的示例显示如何在用户的ImageField中上传和显示图像,那将是最好的。我找不到了。
1.Ther用户模型具有编辑视图profile_edit
,并且可以将图像文件成功上传到字典project_name/media/avatar
。
相关settings.py是:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR,'media').replace('\\','/')+'/'
2.Ther用户模型有一个显示视图profile
,显示用户的一些字段。
3.问题是profile
没有显示头像。这就是外观。
这是它的HTML代码:
<td style="background-color:#FFFF99"><img src="avatar/good.png" width="100" height="100" /></td>
4.成功上传图像文件后,profile_edit
也不会显示头像。
这是它的HTML代码:
<p><label for="id_avatar">头像:</label> 目前: <a href="avatar/good.png">avatar/good.png</a> <input id="avatar-clear_id" name="avatar-clear" type="checkbox" /> <label for="avatar-clear_id">清除</label><br />修改: <input id="id_avatar" name="avatar" type="file" /></p>
如果需要,这是源代码:
#settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR,'media').replace('\\','/')+'/'
#ROOT urls.py
urlpatterns = patterns('',
url(r'^$', 'myauth.views.home', name='home'),
url(r'^accounts/', include('accounts.urls',namespace='accounts')),
url(r'^questions/', include('questions.urls',namespace='questions')),
url(r'^admin/', include(admin.site.urls)),
)
#urls.py
urlpatterns = patterns('accounts.views',
#show the profile
url(r'^users/(?P<user_pk>\d+)/$',
'profile',
{'template_name':'accounts/profile.html'},
name='profile'),
#edit the profile
url(r'^users/(?P<user_pk>\d+)/edit$',
'profile_edit',
{'template_name': 'accounts/profile_edit.html'},
#others ommited...
)
#views.py ,edit the profile
def profile_edit(request,
template_name,
user_pk,
edit_form=ProfileForm,
):
request_user = request.user
if request.method == 'POST':
form = edit_form(data=request.POST, files=request.FILES,instance=request_user)
if form.is_valid():
form.save()
return HttpResponseRedirect(
reverse('accounts:profile',kwargs={'user_pk':user_pk}),
)
else:
form = edit_form(instance=request_user)
context = {
'form': form,
}
return TemplateResponse(request, template_name, context,)
#views.py ,show the profile
def profile(request,
template_name,
user_pk,
):
profile_user = get_object_or_404(User,pk=user_pk)
request_user = request.user
profile_list = [('头像',profile_user.avatar),
#others ommited...
]
context={
'profile_list':profile_list,
}
return TemplateResponse(request, template_name, context,)
#models.py
@python_2_unicode_compatible
class User(AbstractBaseUser, PermissionsMixin):
avatar = models.ImageField(upload_to='avatar',null=True,blank=True,verbose_name=_('头像'))
#rest fields omitted...
#forms.py
class ProfileForm(forms.ModelForm):
class Meta:
model = User
fields = ['avatar','location', 'description', 'signature']
#template.html,edit the profile
<form enctype="multipart/form-data" action="" method="post">{% csrf_token %}
{{form.as_p}}
<input type="submit" value="提交" />
</form>
#template.html,show the profile
<table width="100%">
{% for name,field in profile_list %}
<tr>
<td style="background-color:#FFFF99">{{ name }}</td>
{% if name == "头像" %}
<td style="background-color:#FFFF99"><img src="{{ field }}" width="100" height="100" /></td>
{% else %}
<td style="background-color:#FFFF99">{{ field }}</td>
{% endif %}
</tr>
{% endfor %}
</table>
答案 0 :(得分:1)
django给出的表单条目是访问该图像的直接URL。如果您想知道您的网址配置是否正确,请单击该链接以检查媒体配置。如果您遇到404错误,请尝试@ Ambroise的示例。要访问图片代码中的图片,您需要提供
profile_list = [('头像',profile_user.avatar.url)
请查看此示例以供参考
http://lightbird.net/dbe2/forum.html#edit-profile-template
希望它有所帮助:)