AttributeError: у объекта 'str' нет атрибута 'as_widget'. Как исправить?
Ошибка:
формы:
class CommentForm(ModelForm):
class Meta:
model = Comment
fields = ['text']
labels = {'text': 'Добавить комментарий'}
help_texts = {'text': 'Текст комментария'}
widgets = {
'text': forms.Textarea(attrs={'class': 'your-css-class', 'cols': 80, 'rows': 5})
}
Модели:
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="comments")
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="comments")
text = models.TextField()
created = models.DateTimeField("date published", auto_now_add=True)
def __str__(self):
return self.text
Просмотры:
@login_required
def add_comment(request, username, post_id):
post = get_object_or_404(Post, pk=post_id, author__username=username)
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.post = post
comment.save()
return redirect('post', username=username, post_id=post_id)
else:
form = CommentForm()
return render(request, 'comments.html', {'form': form, 'post': post})
Фильтр:
@register.filter
def addclass(field, css):
return field.as_widget(attrs={"class": css})
{% include 'comments.html' %}
{{ form.text|addclass:"form-control" }}
Как это исправить?