['__class__', '__contains__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_assert_mutable', '_encoding', '_getlist', '_mutable', 'appendlist', 'clear', 'copy', 'dict', 'encoding', 'fromkeys', 'get', 'getlist', 'items', 'keys', 'lists', 'pop', 'popitem', 'setdefault', 'setlist', 'setlistdefault', 'update', 'urlencode', 'values']
When I tried print(self.request.POST.items), I got the blow, for example.
<bound method MultiValueDict.items of <QueryDict: {'csrfmiddlewaretoken': ['qRGlWdPTX9qxpFidwKT8bvY3vWXx5caE2wJZSQTtyWodYSICmW4yMwXG3FAn9oQk'], 'comment': ['I am maru'], 'article': ['2']}>>
I typed the message ('I am maru') and needed to specify the article that I made the message to. I needed to set fields = ('comment','article',) in CommentCreateView, otherwise, I get an error like the below.
RelatedObjectDoesNotExist at /articles/2/comment/
Comment has no article.
Exception Type: | RelatedObjectDoesNotExist |
Exception Value: | Comment has no article. |
Exception Location: | /Users/Koitaro/.local/share/virtualenvs/MMBlog-58h299OP/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py in __get__, line 188 |
Python Executable: | /Users/Koitaro/.local/share/virtualenvs/MMBlog-58h299OP/bin/python |
I tried other listed attributes, but seems like I could not find 'article' information.
When I set the article field and choose an article, I get like 'article': ['2'].
Regarding user, by adding form.instance.author = self.request.user, 'author' was automatically set without choosing it in the form. But, I did not see any author information when I tried several print(self.request.POST.XXX) thing.
When I tried print(self.request.user), the user was printed in terminal.
But, when I tried print(self.request.POST.user), I got an error like below.
AttributeError at /articles/2/comment/
'QueryDict' object has no attribute 'user'
Exception Value: | 'QueryDict' object has no attribute 'user' |
When I set the article field, and choose an article in the comment form, I tried print(form.instance.article). Then, the article I chose was printed in the terminal.
So, seems like form.instance.article is correct.
But how can I set the right part?
form.instance.article = self.response.XXXXX???
Where is the user information?
I successfully can access to user by self.request.user, but currently I have no idea how to access to article information.
Sorry, any advice I can try would be really appreciated!
Looking forward to hearing advice.
Best regards,
Nori