开发者

Storing user's avatar upon registration

I have an extended UserProfile for registering new users. My user_created function connects to signals sent upon registering basic User instance and creates new UserProfile with extended fields from my form. Here's the code :

from registration.signals import user_registered
from accounts.forms import ExtendedRegistrationForm
import accounts
from accounts.models import UserProfile

def user_created(sender, user, request, **kwargs):
    form = ExtendedRegistrationForm(request.POST, request.FILES)
    data = UserProfile(user=user)
    data.is_active = False
    data.first_name = form.data['first_name']
    data.last_name = form.data['last_name']
    data.pid = form.data['pid']
    data.image = form.data['image']
    data.street = form.data['street']
    data.number = form.data['number']
    data.code = form.data['code']开发者_JAVA百科
    data.city = form.data['city']
    data.save()

user_registered.connect(user_created)

Problem is that on this form I have an image field for avatar. As you can see from the code, I'm getting data from form's data list. But apparently imageField does not send it's data with POST request(as I'm getting MultiValueDictKeyError at /user/register/, Key 'image' not found in <QueryDict...) so I can't get it from data[] .

alt text http://img38.imageshack.us/img38/3839/61289917.png If the usual variables are inside 'data', where should I look for files ? Or is the problem more complicated ? Strange thing is that my form doesn't have attribute cleaned_data... I was using dmitko's method here : http://dmitko.ru/?p=546&lang=en . My :

forms : http://paste.pocoo.org/show/230754/

models : http://paste.pocoo.org/show/230755/


You should be validating the form before using it, which will create the "cleaned_data" attribute you're used to. Just check form.is_valid() and the "cleaned_data" attribute will be available, and should contain the file.

The form's "data" attribute is going to be whatever you passed in as its first initalization argument (in this case, request.POST), and files are stored separately in the "files" attribute (whatever you pass in as the second argument, in this case, request.FILES). You don't want to be accessing the form's "data" or "files" attributes directly, as, if you do, you're just reading data straight from the request and not getting any benefit from using forms.


Are you sure the <form enctype="..."> attribute is set to multipart/form-data ? Otherwise the browser is not able to upload the file data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜