关于django:错误’NoneType’对象没有属性’__dict__’

error 'NoneType' object has no attribute '__dict__'

我遇到了这个错误,它不允许我将信息保存在表单中。最初的数据在表单中显示得很好,但是保存对我来说是个挑战。希望有人能帮忙,我真的被卡住了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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

Traceback:

File"C:\Program
Files\Python35\lib\site-packages\django\core\handlers\exception.py" in
inner
41. response = get_response(request)

File"C:\Program
Files\Python35\lib\site-packages\django\core\handlers\base.py" in
_legacy_get_response
249. 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)

File"C:\Program
Files\Python35\lib\site-packages\django\core\handlers\base.py" in
_get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File"C:\Program
Files\Python35\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)

File"C:\Program
Files\Python35\lib\site-packages\django\views\generic\base.py" in
dispatch
88. return handler(request, *args, **kwargs)

File"C:\Program
Files\Python35\lib\site-packages\django\views\generic\edit.py" in post
217. return super(BaseCreateView, self).post(request, *args, **kwargs)

File"C:\Program
Files\Python35\lib\site-packages\django\views\generic\edit.py" in post
183. return self.form_valid(form)

File
"C:\Users\wahab\Desktop\site1\ostra\ostrakodecommerce\posts\views.py"
in form_valid
207. return HttpResponseRedirect(self.get_success_url())

File"C:\Program
Files\Python35\lib\site-packages\django\views\generic\edit.py" in
get_success_url
148. url = self.success_url.format(**self.object.dict)

Exception Type: AttributeError at /profile/-.1/create Exception Value:
'NoneType' object has no attribute 'dict'


您已经重写了form_valid方法,但没有执行该方法执行的任何默认操作,特别是保存对象。

您可以通过调用super方法来解决这个问题,但是没有意义;重定向到success url就是这个方法所做的。完全删除您的form_valid方法,并让现有定义被调用。