Javascript 值通过表单提交,即使在值提交并显示之后,表单中也会显示提交的值
很抱歉标题太长,但实际情况如下: 我提交单词“hello”,然后显示hello。然后在某种形式上,它仍然会说“你好”。我不知道为什么会这样。这是django问题还是需要一些javascript代码来解决…?这是我的完整代码,我认为这是导致这个问题的原因。 在我看来.pyJavascript 值通过表单提交,即使在值提交并显示之后,表单中也会显示提交的值,javascript,jquery,python,django,Javascript,Jquery,Python,Django,很抱歉标题太长,但实际情况如下: 我提交单词“hello”,然后显示hello。然后在某种形式上,它仍然会说“你好”。我不知道为什么会这样。这是django问题还是需要一些javascript代码来解决…?这是我的完整代码,我认为这是导致这个问题的原因。 在我看来.py def post(request, slug): user = get_object_or_404(User,username__iexact=request.user) try:
def post(request, slug):
user = get_object_or_404(User,username__iexact=request.user)
try:
profile = MyProfile.objects.get(user_id=request.user.id)
# if it's a OneToOne field, you can do:
# profile = request.user.myprofile
except MyProfile.DoesNotExist:
profile = None
post = get_object_or_404(Post, slug=slug)
post.views += 1 # increment the number of views
post.save() # and save it
path = request.get_full_path()
comments = Comment.objects.filter(path=path)
#comments = post.comment_set.all()
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid():
parent_id = request.POST.get('parent_id')
parent_comment = None
if parent_id is not None:
try:
parent_comment = Comment.objects.get(id=parent_id)
except:
parent_comment = None
comment_text = comment_form.cleaned_data['comment']
new_comment = Comment.objects.create_comment(
user=MyProfile.objects.get(user=request.user),
path=request.get_full_path(),
text=comment_text,
post = post,
parent = parent_comment)
for c in comments:
c.get_children()
context_dict = {
'post' :post,
'profile' :profile,
'comments' : comments,
'comment_form':comment_form
}
return render(request, 'main/post.html', context_dict)
在我的post.html中
<table class='table'>
{% for comment in comments %}
<tr><td>{{ comment.get_comment }}
<br/><small>via {{ comment.user }} | {{ comment.timestamp|timesince }} ago </small>
{% if not comment.is_child %}
<ul>
{% for child in comment.get_children %}
<li>{{ child.get_comment }}
<small>via {{ child.user }}</small>
</li>
{% endfor %}
</ul>
<a href='#' class='reply_btn'>Reply</a>
<div class='reply_comment'>
<form method="POST" action=''>{% csrf_token %}
<input type='hidden' name='parent_id' value='{{ comment.id }}' />
{{ comment_form.as_p }}
<input type='submit' class='btn btn-default' value='Add reply'/>
</form>
</div>
{% endif %}
</td></tr>
{% endfor %}
</table>
</div>
<div class = "col-sm-3">
</div>
{% include 'footer.html' %}
<script>
{% block jquery %}
$('.reply_btn').click(function(e){
e.preventDefault();
$(this).next(".reply_comment").fadeToggle();
// $(".reply_comment").fadeToggle();
})
{% endblock %}
</script>
{% endblock %}
{注释%中的注释为%}
{{comment.get_comment}}
通过{{comment.user}}{{comment.timestamp}timesince}}ago
{%if not comment.is_child%}
{comment.get_children%}
- {{child.get_comment}}
通过{{child.user}
{%endfor%}
{%csrf_令牌%}
{{comment_form.as_p}}
{%endif%}
{%endfor%}
{%include'footer.html%}
{%block jquery%}
$('.reply_btn')。单击(函数(e){
e、 预防默认值();
$(this).next(“.reply_comment”).fadeToggle();
//$(“.reply_comment”).fadeToggle();
})
{%endblock%}
{%endblock%}
请有人告诉我应该在哪里查看……在检查表单是否有效之前,谢谢您指定表单包含POST数据,以便在返回表单时它仍然包含此数据(以防需要显示错误)。最简单的修复方法是在完成有效逻辑后重新分配表单
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid():
.. Valid logic ..
comment_form = CommentForm()