主要代码
<ul class="list-group" id="barrages_list"></ul>
<script type="text/javascript">
//定时刷新界面(0.5秒)
$(document).ready(function(){
setInterval(function() {
$.get("{% url 'barrages_refresh' %}" + window.location.search,//GET请求的url地址
function(data,status){
$("#barrages_list").html(data);//更新列表内容
});
}, 500);
});
</script>
barrages_refresh.html中的主要代码:
{% for barrage in data %}
<li class="list-group-item">
<h4 style="display:inline-block;">{{ barrage.content }}</h4>
<div style="display:inline-block;float:right;">
<button type="submit" class="btn btn-success pass_button" value="{{ barrage.id }}" style="width:100px;margin-right:30px;">通过</button>
<button type="submit" class="btn btn-danger no_pass_button" value="{{ barrage.id }}" style="width:100px;margin-right:30px;">不通过</button>
</div>
</li>
{% endfor %}
3、views.py中的主要代码:
class BarragesRefresh(APIView):
def get(self):
self.template="organizer/barrages_refresh.html"#渲染的模板
self.check_input('activity_id')
activity_id = self.input['activity_id']
barrages = Barrage.objects.filter(activity_id=activity_id, has_checked_manually=False)
barrages = list(barrages.values("id", "type","content", "openid", "screen_id"))
return barrages
urls.py中的主要代码:
urlpatterns = [
url('barrages/refresh', BarragesRefresh.as_view(),name='barrages_refresh'),
]
jango只是众多网站后端框架中的一种,因为ORM特别好用,现在很受欢迎,如今已经更新到1.8版本,但是很多教程还是在1.7以前的,前一段时间倒腾过sinaapp的云服务,版本依然在1.4,前文有朋友提到过How to tango with django, 确实是很好的入门读物。 之后建议直接读官方文档。我自己很推崇测试驱动开发,也就是所谓的TDD,建议看看Test Driven Development with python这本书。
前端的数据发送与接收
1)提交表单数据
2)提交JSON数据
后端的数据接收与响应
1)接收GET请求数据
2)接收POST请求数据
3)响应请求
如果要给后端传递json数据,就需要增加content-type参数,告诉后端,传递过来的数据格式,并且需要将data转为字符串进行传递。实际上,服务端接收到后,发现是json格式,做的操作就是将字符串转为json对象。
另外,不转为字符串,即使加了content-type的参数,也默认会转成 name=xx&age=1,使后端无法获取正确的json