G
G
Grigory Dikiy2016-01-22 18:05:09
Django
Grigory Dikiy, 2016-01-22 18:05:09

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']

Code in view:
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)

Code in models:
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)

Code in html:
<div class="comment-form">
      <form method="POST" action="/articles/addcomment/{{ article.article_id}}/">
        {% csrf_token %}
        {{ form }}
// ...

PS. I looked at the documentation, but probably not in the right way, I myself am new to Django, as well as to Python. Django: 1.9

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Dikiy, 2016-01-22
@frilix

I found an error: In the form, instead of model, I used models

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question