I made a mistake in view.py. Corrected version is def handleUploadedFile(file): destination = open('usermedia/new.jpg', 'wb+') for chunk in file.chunks(): destination.write(chunk) destination.close() return destination
def user_profile(request, profile_name): owner = get_object_or_404(User, username = profile_name) ownersProfile =get_object_or_404(UserProfile, user = owner) form = ProfileEdit(request.POST) if request.method == 'POST': if form.is_valid(): ownersProfile.about = form.cleaned_data['about'] ownersProfile.avatar = handleUploadedFile(request.FILES['file']) ownersProfile.save() return HttpResponseRedirect("/") else: form = ProfileEdit() Also changed a little bit template: <form method="post" action="./{{owner.username}}"> {{ form.as_p }} <input type="submit" value="Upload" /> </form> But I am getting: MultiValueDictKeyError at /account/profile/test "Key 'file' not found in <MultiValueDict: {}>" :( On Mon, Jun 15, 2009 at 6:54 AM, Oleg Oltar<oltarase...@gmail.com> wrote: > Hi! > > I know that the problem probably was discussed many times already, but > I really can't make it working. > > I read the documentation and prepared the following code: > > model: > > class UserProfile(models.Model): > """ > User profile model, cintains a Foreign Key, which links it to the > user profile. > """ > about = models.TextField(blank=True) > user = models.ForeignKey(User, unique=True) > ranking = models.IntegerField(default = 1) > avatar = models.ImageField(upload_to="usermedia", default = > 'images/js.jpg') > > > def __unicode__(self): > return u"%s profile" %self.user > > > form: > > class ProfileEdit(forms.Form): > about = forms.CharField(label = 'About', max_length = 1000, required=False) > avtar = forms.ImageField() > > > view: > def handleUploadedFile(file): > destination = open('usermedia/new.jpg', 'wb+') > for chunk in file.chunks(): > destination.write(chunk) > destination.close() > return True > > def user_profile(request, profile_name): > owner = get_object_or_404(User, username = profile_name) > ownersProfile =get_object_or_404(UserProfile, user = owner) > form = ProfileEdit(request.POST) > if request.method == 'POST': > form = ProfileEdit(request.POST, request.FILES) > if form.is_valid(): > handleUploadedFile(request.FILES['file']) > else: > form = ProfileEdit() > > > > data = RequestContext(request, > { > 'owner' : owner, > 'ownersProfile' : ownersProfile, > 'form' : form > } > ) > return render_to_response('registration/profile.html', data) > > > And part of template: > > <form method="post" action="."> > {{ form.as_p }} > <input type="submit" value="Upload" /> > </form> > > > > After trying this code in browser I am getting a text field with > browse button, so when I chose file to upload, and finally click > upload I am getting 404 Error. > Not sure what I might doing wrong. > Please help > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---