django的可重用应用程序很好地连接了客户端和服务器端。
django-xross的Python项目详细描述
https://github.com/idlesign/django-xross
说明
django的可重用应用程序很好地连接了客户端和服务器端。
在HTML中使用一些声明性技术简化服务器和客户端的交互, 在你的观点中有几个xross函数。
在您的视图中的某个位置。py:
fromdjango.shortcutsimportrenderfromxross.toolboximportxross_view,xross_listener# That's all we need from xross.deflist_news(request):"""This function will be used by xross to load news using AJAX."""news=...# Here we fetch some news from DB.returnrender(request,'mytemplates/sub_news.html',{'news':news})@xross_view(list_news)# Decorate your view - instruct xross to use `list_news` when needed.defindex_page(request):"""This is our view to streamline."""xross_listener()# xross will handle AJAX calls from that moment.returnrender(request,'mytemplates/index.html')
现在转到您的mytemplates/index.html:
<!DOCTYPE html><html><head><!-- xross depends on jQuery. Include it. --><scriptsrc="http://yandex.st/jquery/2.1.1/jquery.min.js"></script><!-- Now xross itself. --><scriptsrc="{{ STATIC_URL }}js/xross/xross.min.js"></script><scripttype="text/javascript">xross.automate();// Instruct xross to watch for page elements with `xross` class. </script></head><body><divid="list_news"class="xross"><!-- Contents of this div will be replaced with news from Django's `list_news()` automatically on page load. That's the default of xross, but it knows some other nice little tricks. Read the docs. --></div></body></html>
最后mytemplates/sub_news.html(没什么特别的):
{% for item in news %} <div><div>{{ item.title }}</div><div>{{ item.text }}</div></div> {% endfor %}
不是全部,不是全部。阅读文档!