上传图片到用户个人资料

时间:2015-05-01 06:26:57

标签: django django-uploads

我第一次尝试制作自定义用户个人资料。一切都很好,除了ImageField。

当我尝试选择图像并点击模板页面上的“更新”按钮时 - 图像不会上传到目标文件夹,我也看不到数据库更改...

我需要写一些特别的东西吗?

我的模型userprofile

# -*- coding: utf-8 -*- 

import PIL

from django.db import models
from django.contrib.auth.models import User
from PIL import Image
from django.db import models

# Create your models here.

class UserProfile(models.Model):  
    user = models.OneToOneField(User)
    user_picture = models.ImageField(upload_to='users', blank=True, null=True)
    user_balance = models.IntegerField(default=0)

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u) [0])

我的forms.py

# -*- coding: utf-8 -*- 

from django import forms
from userprofile.models import User
from userprofile.models import UserProfile

class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('email', 'first_name', 'last_name',)

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('user_picture', 'user_balance')

我的views.py

# -*- coding: utf-8 -*- 

from django.shortcuts import render, render_to_response, redirect
from django.shortcuts import HttpResponseRedirect, Http404, HttpResponse
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.core.context_processors import csrf

# Create your views here.
from userprofile.forms import UserForm
from userprofile.forms import UserProfileForm

def userprofile(request):
    if request.POST:
        user_form = UserForm(request.POST, instance=request.user)
        user_profile = UserProfileForm(request.POST, request.FILES, instance=request.user.profile)
        if user_form.is_valid() and user_profile.is_valid():
            user_form.save()
            user_profile.save()
    else:
        user_form = UserForm(instance=request.user,
            initial={
                'first_name': request.user.first_name,
                'last_name': request.user.last_name,
                'email': request.user.email,
            })
        user = request.user
        profile = user.profile
        user_profile = UserProfileForm(instance=profile)

    return render_to_response('profile.html', {'user_form': user_form, 'user_profile': user_profile}, context_instance=RequestContext(request))

我的模板:

{% for field in form %}
    {{ field.error }}

{% endfor %}

<form action="/users/profile/" method="POST">{% csrf_token %}
    {{ user_form.as_ul }}
    {{ user_profile.as_ul }}

    <input type="submit" value="Update" />

</form>

我的urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',

    url(r'^accounts/', include('allauth.urls')),

    url(r'^admin/', include(admin.site.urls)),

    (r'^tinymce/', include('tinymce.urls')),

    url(r'^$', 'expert.views.index', name='index'),
    url(r'^(\d+)/?$', 'expert.views.index'),

    url(r'^articles/all/$', 'article.views.articles', name='articles'),
    url(r'^articles/get/(?P<article_id>\d+)/$', 'article.views.article', name='article'),
    url(r'^articles/addcomment/(?P<article_id>\d+)/$', 'article.views.addcomment', name='addcomment'),
    url(r'^page/(\d+)/$', 'article.views.articles'),

    url(r'^experts/all/$', 'expert.views.experts', name='experts'),
    url(r'^experts/get/(?P<expert_id>\d+)/$', 'expert.views.expert', name='expert'),

    url(r'^call/$', 'userform.views.userform', name='call'),

    url(r'^users/profile/$', 'userprofile.views.userprofile', name='userprofile'),

) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

1 个答案:

答案 0 :(得分:0)

您是否在settings.py和urls.py中添加了任何字段?

如果没有尝试添加:

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')

到settings.py和:

if settings.DEBUG:
urlpatterns += patterns('',
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT,
    }),
   )

static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

到你的urls.py