2017-04-05 64 views
0

我試圖在註冊用戶後創建用戶配置文件。在此配置文件中,我需要上傳圖片。但是在驗證圖像時我一直在收到KeyError。用Django CBV上傳圖像CreateView

經過幾個小時的谷歌搜索和嘗試不同的解決方案,我現在卡住了。有人可以告訴我我做錯了什麼嗎?提前致謝。

models.py

class Profile(models.Model): 
    user = models.OneToOneField(User, 
           on_delete=models.CASCADE) 
    slug = models.SlugField(default='', 
          editable=False) 
    picture = models.ImageField('Profile picture', 
           upload_to='profile_pics/%Y-%m-%d/', 
           null=True, 
           blank=True) 
    bio = models.CharField("Short Bio", max_length=200, blank=True, null=True) 
    email_verified = models.BooleanField("Email verified", default=False) 

views.py

from .models import Profile 
from .forms import ProfileForm 

class CreateProfile(FormView): 
    model = Profile 
    template_name = 'profiles/profile_form.html' 
    form_class = ProfileForm 
    fields = ['picture', 'bio'] 

    def get_success_url(self): 
     return reverse('profiles:detail', kwargs={'slug': self.slug}) 


    def form_valid(self, form): 
     profile = form.save(commit=False) 
     image = form.cleaned_data['image'] 
     obj.user = self.request.user 
     profile.save() 

     return HttpResponseRedirect(self.get_success_url()) 

forms.py

from .models import Profile 


class ProfileForm(forms.ModelForm): 
    class Meta: 
     model = Profile 
     fields = ['picture', 'bio',] 

回溯:

File "/home/xxxxxxxxxx/xxxxxxxx/xxxxxxx/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner 
    42.    response = get_response(request) 

File "/home/xxxxxxxxxx/xxxxxxxx/xxxxxxx/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 
    187.     response = self.process_exception_by_middleware(e, request) 

File "/home/xxxxxxxxxx/xxxxxxxx/xxxxxxx/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 
    185.     response = wrapped_callback(request, *callback_args, **callback_kwargs) 

File "/home/xxxxxxxxxx/xxxxxxxx/xxxxxxx/lib/python3.5/site-packages/django/views/generic/base.py" in view 
    68.    return self.dispatch(request, *args, **kwargs) 

File "/home/xxxxxxxxxx/xxxxxxxx/xxxxxxx/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch 
    88.   return handler(request, *args, **kwargs) 

File "/home/xxxxxxxxxx/xxxxxxxx/xxxxxxx/lib/python3.5/site-packages/django/views/generic/edit.py" in post 
    183.    return self.form_valid(form) 

File "/home/xxxxxxxxxx/xxxxxxxx/xxxxxxx/template/profiles/views.py" in form_valid 
    28.   image = form.cleaned_data['image'] 

Exception Type: KeyError at /profiles/create/ 
Exception Value: 'image' 
+2

更換

image = form.cleaned_data['image'] 

。所以必須是'form.cleaned_data ['picture']',對吧? – Enix

回答

0

的錯誤是在這裏:

def form_valid(self, form): 
    profile = form.save(commit=False) 
    image = form.cleaned_data['image'] 
    obj.user = self.request.user 
    profile.save() 

您嘗試從form.cleaned_data獲得「形象」,而你的頭像場被稱爲picture

所以,隨着您已經定義圖像字段名`picture`,而不是'在你的模型image`

image = form.cleaned_data['picture'] 
+0

謝謝。這工作。有一個upvote。 – user2901792