2017-07-16 122 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方法,并调用现有的定义。

相关问题