urls.py:
url(r'^articles/addcomment/(?P<article_id>\d)/+$', 'article.views.addcomment'),
<form action = '/articles/addcomment/{{ article.id }}/' method="post"> {% csrf_token %} {{ form }} <input type = 'submit' class = 'button' value="Добавить комментарий"> </form>
class Comments(models.Model): class Meta: db_table = 'comments' text = models.TextField(verbose_name='Текст комметария') comments_article = models.ForeignKey(Article)
from django.forms import ModelForm from article.models import Comments class CommentForm(ModelForm): class Meta: model = Comments fields = '__all__' exclude = ['comments_article']
def addcomment(request, article_id): if request.POST: form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.comments_article = Article.objects.get(id=article_id) form.save() return redirect('/articles/get/%s/' % article_id)