Answer the question
In order to leave comments, you need to log in
Django: ModelForm has no model class specified?
Good day! I understand from the video in Django youtube . But there was a problem: ModelForm has no model class specified .
Form code:
from article.models import Comments
from django.forms import ModelForm
class CommentForm(ModelForm):
class Meta:
models = Comments
fields = ['comments_text', 'comments_author']
def article(request, article_id=1):
args = {}
args.update(csrf(request))
args['article'] = Article.objects.get(id=article_id)
args['comments'] = Comments.objects.filter(comments_article_id=article_id)
args['form'] = CommentForm
return render_to_response('article.html', args)
class Comments(models.Model):
class Meta:
db_table = 'comments'
comments_text = models.TextField()
comments_author = models.CharField(max_length=100, default="Неизвестный пользователь")
comments_article = models.ForeignKey(Article)
<div class="comment-form">
<form method="POST" action="/articles/addcomment/{{ article.article_id}}/">
{% csrf_token %}
{{ form }}
// ...
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question