If you don't want to display article and author in the template. Use custom
form design instead of {% form.as_p %} tag.

Something like <input name={{ form.comment }} value = {{ form.comment }} >
this will render only comment field on template. You should not remove
those fields as those are required to process form.

Share the code as zip will exam it and let you know.

On Sun, 28 Apr, 2019, 7:10 PM Atsunori Kaneshige, <atsun...@umich.edu>
wrote:

> Hi Britto,
>
> Thank you for your reply.
> It's been working fine when I add ('comment', 'article',).
> The fields for CommentCreateView used to be ('comment',
> 'article','author',)
> But I wanted to get rid of 'author' and 'article' from comment form.
>
> def form_valid(self, form):
> form.instance.author = self.request.user
>      return super().form_valid(form)
>
>
> On Sunday, April 28, 2019 at 1:10:24 AM UTC-4, SimpleHumble wrote:
>>
>> set form.instance.article = request.POST.get('article')
>> and add article in fields too like fields = ('comment', 'article')
>> it should work flawlessly.
>>
>>
>> Regards
>> Britto
>>
>>
>> On Sun, 28 Apr 2019 at 09:30, Atsunori Kaneshige <atsu...@umich.edu>
>> wrote:
>>
>>> Hi, Britto,
>>>
>>> Thank you very much for your reply!
>>> I tried form.instance.article = *self.*request.POST.get('article'), I
>>> still got an error shown below.
>>>
>>>
>>> RelatedObjectDoesNotExist at /articles/2/comment/
>>>
>>> Comment has no article.
>>>
>>> Request Method: POST
>>> Request URL: http://127.0.0.1:8000/articles/2/comment/
>>> Django Version: 2.1.5
>>> 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
>>> Python Version: 3.6.5
>>>
>>>
>>> When I tried print(dir(self.request.POST)), I got the below.
>>>
>>>
>>> ['__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.
>>>
>>> Request Method: POST
>>> Request URL: http://127.0.0.1:8000/articles/2/comment/
>>> Django Version: 2.1.5
>>> 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'
>>>
>>> Request Method: POST
>>> Request URL: http://127.0.0.1:8000/articles/2/comment/
>>> Django Version: 2.1.5
>>> Exception Type: AttributeError
>>> 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
>>>
>>> On Saturday, April 27, 2019 at 10:41:14 PM UTC-4, SimpleHumble wrote:
>>>>
>>>> The best way to debug it is add a print function with dir function just
>>>> before the error occurring line and see what are available attributes in
>>>> request.POST object like this print(dir("========", request.POST,
>>>> "========")) . Also without dir function will show what is available in
>>>> POST.
>>>> Look at the terminal after running development server and your action
>>>> to trigger error, see what are the attributes available for your request.
>>>>
>>>> Probably you have to set form.instance.article = 
>>>> request.POST.get('article')
>>>> would work.
>>>>
>>>> Regards
>>>> Britto
>>>>
>>>>
>>>> On Sun, 28 Apr 2019 at 02:56, Atsunori Kaneshige <atsu...@umich.edu>
>>>> wrote:
>>>>
>>>>> Sorry, when I make a comment, I get this error.
>>>>>
>>>>> Any advice would be really appreciated!
>>>>>
>>>>>
>>>>>
>>>>> AttributeError at /articles/2/comment/
>>>>>
>>>>> 'WSGIRequest' object has no attribute 'article'
>>>>>
>>>>> Request Method: POST
>>>>> Request URL: http://127.0.0.1:8000/articles/2/comment/
>>>>> Django Version: 2.1.5
>>>>> Exception Type: AttributeError
>>>>> Exception Value:
>>>>>
>>>>> 'WSGIRequest' object has no attribute 'article'
>>>>>
>>>>> Exception Location: 
>>>>> /Users/Koitaro/Desktop/Web_Development/MMBlog/articles/views.py
>>>>> in form_valid, line 75
>>>>> Python Executable:
>>>>> /Users/Koitaro/.local/share/virtualenvs/MMBlog-58h299OP/bin/python
>>>>> Python Version: 3.6.5
>>>>> Python Path:
>>>>>
>>>>> ['/Users/Koitaro/Desktop/Web_Development/MMBlog',
>>>>>  
>>>>> '/Users/Koitaro/.local/share/virtualenvs/MMBlog-58h299OP/lib/python36.zip',
>>>>>  '/Users/Koitaro/.local/share/virtualenvs/MMBlog-58h299OP/lib/python3.6',
>>>>>  
>>>>> '/Users/Koitaro/.local/share/virtualenvs/MMBlog-58h299OP/lib/python3.6/lib-dynload',
>>>>>  '/Applications/anaconda3/lib/python3.6',
>>>>>  
>>>>> '/Users/Koitaro/.local/share/virtualenvs/MMBlog-58h299OP/lib/python3.6/site-packages']
>>>>>
>>>>> Server time: Sat, 27 Apr 2019 21:24:45 +0000
>>>>>
>>>>>
>>>>> On Saturday, April 27, 2019 at 5:07:19 PM UTC-4, Atsunori Kaneshige
>>>>> wrote:
>>>>>>
>>>>>> I really appreciate if anybody could give me an advice about
>>>>>> form_valid.
>>>>>>
>>>>>> Here is what I want to do.
>>>>>> I am making a simple blog.
>>>>>> I am making simple features:
>>>>>> 1) only superuser can create articles.
>>>>>> 2) logged-in users can make comments to articles.
>>>>>>
>>>>>> I followed along tutorials and mostly done.
>>>>>> *But last thing I want to do is that when users make comments to any
>>>>>> articles, I want author field and article field to be automatically set.*
>>>>>> *Currently, users need to choose author field and article field to
>>>>>> make comments as well as comment texts.*
>>>>>>
>>>>>> The tutorial that I followed along uses form_valid and by using
>>>>>> form_valid, now I don't need to choose author.
>>>>>> But I have been struggling with how to automatically set article
>>>>>> field by using form_valid.
>>>>>>
>>>>>> I have a simple models.py and views.py below.
>>>>>>
>>>>>> <models.py>
>>>>>>
>>>>>> class Article(models.Model):
>>>>>> title = models.CharField(max_length=255)
>>>>>> body = models.TextField()
>>>>>> date = models.DateTimeField(auto_now_add=True)
>>>>>> author = models.ForeignKey(
>>>>>> get_user_model(),
>>>>>> on_delete=models.CASCADE,
>>>>>> )
>>>>>>
>>>>>> def __str__(self):
>>>>>> return self.title
>>>>>>
>>>>>> def get_absolute_url(self):
>>>>>> return reverse('article_detail', args=[str(self.id)])
>>>>>>
>>>>>> class Comment(models.Model): # new
>>>>>>     article = models.ForeignKey(
>>>>>>     Article,
>>>>>>     on_delete=models.CASCADE,
>>>>>>     related_name = 'comments',
>>>>>>     )
>>>>>>     comment = models.CharField(max_length=140)
>>>>>>     author = models.ForeignKey(
>>>>>>         get_user_model(),
>>>>>>         on_delete=models.CASCADE,
>>>>>>     )
>>>>>>
>>>>>>     def __str__(self):
>>>>>>         return self.comment
>>>>>>
>>>>>>     def get_absolute_url(self):
>>>>>>         return reverse('article_list')
>>>>>>
>>>>>> <views.py>
>>>>>> ...(not showing all)
>>>>>> #for comment
>>>>>> class ArticleCommentCreateView(LoginRequiredMixin, CreateView):
>>>>>> model = Comment
>>>>>> template_name = 'article_comment_new.html'
>>>>>> fields = ('comment',)
>>>>>> login_url = 'login'
>>>>>>
>>>>>> def form_valid(self, form):
>>>>>> form.instance.author = self.request.user
>>>>>> *form.instance.article = self.request.article*
>>>>>> return super().form_valid(form)
>>>>>>
>>>>>> I keep getting errors with this code.
>>>>>> I know that *form.instance.article = self.request.article* is
>>>>>> something wrong, but I am having hard time to figure out how to set 
>>>>>> article
>>>>>> field to be automatically set.
>>>>>>
>>>>>>
>>>>>> Please give any advice about this.
>>>>>> I really appreciate.
>>>>>>
>>>>>> Looking forward to hearing from anyone.
>>>>>>
>>>>>> Best regards,
>>>>>>
>>>>>> Nori
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Django users" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to django...@googlegroups.com.
>>>>> To post to this group, send email to django...@googlegroups.com.
>>>>> Visit this group at https://groups.google.com/group/django-users.
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/django-users/352ed4ec-fcf8-4590-ae0b-9c97e29d80ef%40googlegroups.com
>>>>> <https://groups.google.com/d/msgid/django-users/352ed4ec-fcf8-4590-ae0b-9c97e29d80ef%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/220604e7-a433-4771-b52e-b9eede1b61e4%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/220604e7-a433-4771-b52e-b9eede1b61e4%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/2a71817a-10fa-41f5-8514-8c7d952952f6%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/2a71817a-10fa-41f5-8514-8c7d952952f6%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAF0v3b7yFbfEoLZpXQaGRY-P4JYP8T325tQvmDdbnB-1hio%3D4Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to