tcpiper
tcpiper

Reputation: 2554

django 1.6 Why can't I display avatars?

First of all, if there is a complete example showing how to upload and display image in a User's ImageField, that would be the best. I can't find it.

1.Ther User model have an edit view profile_edit, and it can successfully upload image file to the dictionary project_name/media/avatar.

enter image description here

Related settings.py is:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR,'media').replace('\\','/')+'/'

2.Ther User model have an show view profile, which shows some fields of the User.

3.Problem is that the profile doesnot display the avatar.This is the look. enter image description here

this is its html code: <td style="background-color:#FFFF99"><img src="avatar/good.png" width="100" height="100" /></td>

4.After successfully uploading an image file, the profile_edit also doesnot display avatar. enter image description here

this is its html code: <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>

This is the source code, if needed:

#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>

Upvotes: 0

Views: 303

Answers (1)

Shrinath Shenoy
Shrinath Shenoy

Reputation: 160

The form entry given by django is direct url to access that image. If you want to know is your urls are configured properly then click on that link to check the media configuration. If you get 404 error try the @Ambroise's example. To access image in your image tag you need to provide

profile_list = [('头像',profile_user.avatar.url)

Check this example for your reference

http://lightbird.net/dbe2/forum.html#edit-profile-template

hope it helps :)

Upvotes: 1

Related Questions