2017-07-16 125 views
0

我已經遇到了這個錯誤,它不讓我保存表單中的信息。初始數據在表格中顯示得很好,但保存對我很有挑戰。希望有人能幫助,我真的堅持錯誤'NoneType'對象沒有屬性'__dict__'

class UserPostCreatView(CreateView): 
    form_class = PostModelForm 
    template_name = 'posts/post_form.html' 
    success_url = "/profile/{user_slug}/wall" 



def get_initial(self): 
    # Get the initial dictionary from the superclass method 
    initial = super(UserPostCreatView, self).get_initial() 
    user_slug = self.kwargs.get('user_slug') 
    user_content_type = ContentType.objects.get_for_model(authomodel.User) 
    auth_user = get_object_or_404(authomodel.User, user_slug=user_slug) 
    auth_user_id = auth_user.id 
    # Copy the dictionary so we don't accidentally change a mutable dict 
    initial = initial.copy() 
    initial = { 
    "content_type": user_content_type, 
    "object_id" : auth_user_id, 
    } 
    return initial 

def form_valid(self, form): 
    return HttpResponseRedirect(self.get_success_url()) 





def get_form_kwargs(self): 
    """ 
    Returns the keyword arguments for instantiating the form. 
    """ 
    kwargs = { 
     'initial': self.get_initial(), 
    } 

    if self.request.method in ('POST', 'PUT'): 
     kwargs.update({ 
      'data': self.request.POST or None, 
      'files': self.request.FILES or None}) 
    return kwargs 


def get_form_class(self): 
    return self.form_class 

回溯:

文件「C:\ PROGRAM 文件\ Python35 \ LIB \站點包\ Django的\核心\處理器\例外。 PY」在 內 41.響應= get_response(請求)

文件 「C:\程序 文件\ Python35 \ lib中\站點包\ django的\芯\處理程序\ base.py」 在 _legacy_get_response 249.響應= self._get_response(請求)

文件 「C:\ PROGRAM 文件\ Python35 \ LIB \站點包\ Django的\核心\處理器\ base.py」 在 _get_response 187.響應=自.process_exception_by_middleware(E,請求)

文件 「C:\程序 文件\ Python35 \ lib中\站點包\ django的\芯\處理程序\ base.py」 在 _get_response 185.響應= wrapped_callback(請求, * callback_args,** callback_kwargs)

文件「C:\ Program 文件\ Python35 \ LIB \站點包\ Django的\ 「鑑於 68回self.dispatch(請求,* ARGS,** kwargs)

文件」 意見\通用\ base.py C:\ PROGRAM 文件\ Python35 \ LIB \站點包\ Django的\意見\通用\ base.py 「在 調度 88回處理器(請求,* ARGS,** kwargs)

文件」 C:\ PROGRAM 文件(請求,* args,** kwargs)

文件「C」中的\ Python35 \ lib \ site-packages \ django \ views \ generic \ edit.py「返回超級(BaseCreateView,self).post :\ PROGR我 文件\ Python35 \ LIB \站點包\ Django的\意見\通用\ edit.py 「在後 183回self.form_valid(形式)

文件 」 C:\用戶\瓦哈卜\桌面\ site1的\奧斯特拉\ ostrakodecommerce \ \職位的views.py 「 在form_valid 207返回HttpResponseRedirect(self.get_success_url())

文件」 C:\ PROGRAM 文件\ Python35 \ LIB \站點包\ Django的\ views \ generic \ edit.py「 get_success_url 148. url = self.success_url.format(** self.object。 字典

異常類型:AttributeError的在/profile/-.​​1/create異常值: 'NoneType' 對象有沒有屬性 '字典'

+0

嘗試打印user_slug,我猜它會出現空白 – Exprator

+0

實際上,一個做得很好,它提供的初始數據,其形式很好地顯示在 – dungu

+0

形式很好保存數據已經存在表格 – dungu

回答

0

您已經覆蓋了form_valid方法,但沒有執行該方法執行的任何默認操作,特別是保存該對象。

你可以通過調用super方法來解決這個問題,但是有沒有意思;重定向到成功的網址就是這個方法所做的。完全刪除form_valid方法,並調用現有的定義。

相關問題