我正在尝试使用邮寄表单中的textarea向Django发送评论,但我的Django表单没有收到任何东西。
HTML
<form name='commentForm' method='post'>
{% csrf_token %}
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<textarea name="commentt"rows=1 class="mdl-textfield__input" id="comment" form="commentForm"></textarea>
<label for="comment" class="mdl-textfield__label">Join the discussion</label>
</div>
<button type="submit" class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon">
<i class="material-icons" role="presentation">check</i><span class="visuallyhidden">add comment</span>
</button>
</form>
Django Forms.py
class CommentForm(forms.ModelForm):
comment = forms.CharField()
class Meta:
model = Comment
fields = ('comment',)
Django views.py
class EntryView(TemplateView):
template_name = 'post/entry.html'
def get(self, request):
commentq = Comment.objects.all()
form = CommentForm()
args = {'commentq' : commentq, 'form' : form }
return render(request, self.template_name, args)
def post(self, request):
form = CommentForm(request.POST["commentt"])
if form.is_valid():
text = form.cleaned_data['comment']
new_comment = Comment(comment_id = 1, comment_text = text, user = 'hedgehog', comment_date = timezone.now())
new_comment.save()
form = CommentForm()
url = '/post/entry'
return HttpResponseRedirect(url)
args = {'form' : form }
return render(request, self.template_name, args)