Please share the form and form template as well.

On Sun, 28 Apr 2019, 02:37 Atsunori Kaneshige <[email protected] 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 [email protected].
> To post to this group, send email to [email protected].
> 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/fe3a8f1d-2e97-41f8-8af4-fe638cb9e787%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/fe3a8f1d-2e97-41f8-8af4-fe638cb9e787%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 [email protected].
To post to this group, send email to [email protected].
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/CABCRpYPSLS1tYJRv23LiQM6LfaFQhvLbinVbXoX2cnnYmaDrcA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to