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-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/352ed4ec-fcf8-4590-ae0b-9c97e29d80ef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to