我无法将 传递form id
给 views.py 页面。jQuery 不会将“id”传递给视图页面。请告诉我我做错了什么。
这是我的 jQuery。
$(function(){
$('.chatroom').submit(function () {
$.ajax({
type: "POST",
url: "/dashboard",
data : {
'chatroom_id' : $(this).attr('id')
},
});
});
这是我的模板
{% for key, values in chat_data.items %}
<div class="container-fluid" alt = {{key}}>
<div class="row-fluid">
<div class="span2">
{{values.from}} <br/> {{values.init_query}}
</div>
<div class="span10 well">
{% for k in values.chat %}
<label> Text : {{k.text}} </label>
<label> {{k.date_time}} </label>
{% endfor %}
<form action = "#" method = "POST" id = {{key}} class="chatroom">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value = "Sent" class="btn btn-primary">
</form>
</div>
</div>
</div>
{% endfor %}
视图.py
if request.is_ajax():
if request.method == "POST":
chatroom_id = request.POST['chatroom_id']
else:
chatroom_id =''
print chatroom_id
当我删除if request.is_ajax()
条件时,它会显示错误消息"Key 'chatroom_id' not found in <QueryDict: {u'reply': [u''], u'csrfmiddlewaretoken': [u'yIJct9O7WfyPnWmDosW9N5TEklRwoIHP']}>"