I'v пыталась использовать форму для вставки комментария. Когда пользователь вводит комментарий, он получает номер статьи, имя пользователя и тело комментария и перенаправляет на предыдущую страницу. Однако он продолжает выводить сообщение об ошибке, и я не смог точно определить, какую часть я пропустил.Django: Ошибка при создании объектов с использованием
Это model.py
class Comment(models.Model):
article_no = models.IntegerField(default=1)
comment_no = models.AutoField(primary_key=True)
comment_writer = models.CharField(max_length=50)
comment_body = models.CharField(max_length=300)
comment_date = models.DateTimeField(editable=False, default=datetime.now())
forms.py
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['article_no', 'comment_body', 'comment_writer']
view.py
@login_required
def comment(request, article_no):
user = request.user
request.session['username'] = user.username
if 'username' is None:
return render(request, 'blog/home.html')
else:
if request.POST.has_key('comment_body') == False:
return HttpResponse('comment is none')
else:
if len(request.POST['comment_body']) == 0:
return HttpResponse('comment is none')
else:
comment_body = request.POST['comment_body']
print(comment_body)
if request.POST.has_key('comment_writer') == False:
return HttpResponse('writer is none')
else:
if len(request.POST['comment_writer']) == 0:
return HttpResponse('comment is none')
else:
comment_writer = request.POST['comment_writer']
print(comment_writer)
try:
instance = CommentForm(Comment_body=comment_body, Comment_writer=comment_writer, Article_no=article_no)
instance.save()
instance.Comment += 1
instance.save()
#return HttpResponse('comment added')
item = get_object_or_404(Article, pk=article_no)
return render(request, 'blog/detail.html', {'item': item})
except:
print("A")
return HttpResponse('error')
print("B")
return HttpResponse('error')
urls.py
url(r'^comment/(?P<article_no>[0-9]+)/$', views.comment, name='comment'),
Какое сообщение об ошибке отображается? –
опубликуйте, что вы получаете? – Cadmus
@EvansMurithi Он говорит: «__init __() получил неожиданный аргумент ключевого слова« comment_body ». Поэтому я подумал, что это может быть вызвано помещением заглавной буквы на имя поля, поэтому я изменил ее на более низкую. Однако результат тот же. – camila