mardi 5 mai 2015

'MultiValueDict' object is not callable

I am trying to have users upload an image in my django web app. I've looked through the forum and everyone is asking for several layers of complexity that I'm not wanting to tackle yet. Keep it simple....

The goal is to avoid forms if it is possible. The error comes specifically from the views line that says: profile.picture = request.FILES('profilepic')

Model:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    website = models.URLField(blank=True, null=True)
    facebook = models.URLField(blank=True, null=True)
    twitter = models.URLField(blank=True, null=True)
    linkedin = models.URLField(blank=True, null=True)
    bio = models.TextField(max_length=500, blank=True, null=True)
    address = models.OneToOneField(Address, blank=True, null=True)
    picture = models.ImageField(upload_to='/media/profile_images', blank=True)

Views:

def registration(request):
    if request.POST:
        email = request.POST['email']
        username = request.POST['email']
        password = request.POST['password']
        user = User.objects.create_user(username=username, password=password, email=email)
        user.save()
        profile = UserProfile()
        u = User.objects.get(username=username)
        profile.picture = request.FILES('profilepic')
        profile.user = u
        profile.save()
        new_member = authenticate(username=username, password=password)

    if new_member:
        login(request, new_member)
        return HttpResponseRedirect('/app/')
    return HttpResponseRedirect('/app/')

return render(request, 'app/registration.html')

Template:

<form method="POST" id="registration">    
<div id="errors" style="color: red;"></div> 
{% csrf_token %}    
    Email: <input type="email" name="email" id="email"><br>    
    Password: <input type="password" name="password" id="password"><br>    
    Profile picture: <input type="file" name="profilepic" id="profilepic"> 
<input type="submit" value="Register"> 
</form>

2 commentaires:

  1. profile.picture = request.FILES('profilepic')

    should be like this:
    profile.picture = request.FILES['profilepic']

    because your are calling a dict value.

    RépondreSupprimer