2
votes

I have encoutered this error and its not letting me save the info in the form. The initial data is showing well in the form but saving is challenging me. Hope someone can help, I'm really stuck

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'

1
try to print the user_slug, i guess its coming blankExprator
Actually that one is doing fine and its the one supplying the initial data, which is showing well in the formdungu
The challenge comes in saving the data which is already existing in the formsdungu
Please post the full traceback, which shows where this error is happening.Daniel Roseman
@ Daniel Roseman traceback posteddungu

1 Answers

2
votes

You have overridden the form_valid method but haven't done any of the default actions performed by that method, in particular saving the object.

You could fix this by calling the super method, but there is no point; redirecting to the success url is what that method does anyway. Remove your form_valid method altogether and let the existing definition be called.