首页 文章

错误'NoneType'对象没有属性'__dict__'

提问于
浏览
1

我遇到了这个错误,并没有让我在表单中保存信息 . 初始数据在表格中表现良好,但节省对我构成挑战 . 希望有人可以提供帮助,我真的被困住了

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 Files \ Python35 \ lib \ site-packages \ django \ core \ handlers \ exception.py”.response = get_response(request)文件“C:\ Program Files \ Python35 \ lib _legacy_get_response 249中的\ site-packages \ django \ core \ handlers \ base.py“ . response = self._get_response(request)File”C:\ Program Files \ Python35 \ lib \ site-packages \ django \ core \ handlers \ base .py“in _get_response 187. response = self.process_exception_by_middleware(e,request)文件”C:\ Program Files \ Python35 \ lib \ site-packages \ django \ core \ handlers \ base.py“in _get_response 185. response = wrapped_callback (request,* callback_args,** callback_kwargs)视图68中的文件“C:\ Program Files \ Python35 \ lib \ site-packages \ django \ views \ generic \ base.py” . 返回self.dispatch(request,* args, ** kwargs)文件“C:\ Program Files \ Python35 \ lib \ site-packages \ django \ views \ generic \ base.py”在dispatch 88中 . 返回处理程序(request,* args,** kwargs)文件“C:在第217页中的\ Program Files \ Python35 \ lib \ site-packages \ django \ views \ generic \ edit.py“ret urn super(BaseCreateView,self).post(request,* args,** kwargs)文件“C:\ Program Files \ Python35 \ lib \ site-packages \ django \ views \ generic \ edit.py”在帖子183.返回form.valid 207中的self.form_valid(form)文件“C:\ Users \ wahab \ Desktop \ site1 \ ostra \ ostrakodecommerce \ posts \ views.py” . 返回HttpResponseRedirect(self.get_success_url())文件“C:\ Program Files \ get_success_url中的Python35 \ lib \ site-packages \ django \ views \ generic \ edit.py“ . url = self.success_url.format(** self.object.dict)异常类型:/profile/-.1/中的AttributeError创建异常值:'NoneType'对象没有属性'dict'

1 回答

  • 1

    您已重写 form_valid 方法但尚未执行该方法执行的任何默认操作,特别是保存对象 .

    你可以通过调用super方法来解决这个问题,但没有意义;无论如何,重定向到成功URL是该方法所做的 . 完全删除 form_valid 方法并调用现有定义 .

相关问题