严格的说这个话题应该属于一个html前端路径加载问题。为了实现一个局部更新页面的功能,简单了解了一下Ajax。Ajax是一个为了实现浏览器和服务器异步通信功能的模块。严格来说不是一个新的语言,只是JS的一个延伸。而jQuery是一个JS的类库,里面也包含了Ajax的一些功能,比直接写Ajax更简单些。
看了些资料后,在网上copy了一个简单的例子做测试:
<script src="../static/jQuery/jquery.min.js">
</script> <script>
$(document).ready(
function(){
$("#btn1").click(function(){
$('#test').load("/demo_test.txt");
})
}
)
</script> <h3 id="test">jQuery AJAX 改变这段文本。</h3>
<button id="btn1" type="button">get some extra content</button>
这个例子实现了一个简单的功能,点击按钮之后内弄会被demo_test.txt 里面的内容替换掉。
代码分析:1.导入jquery.min.js库,jQuer源地址://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js 。
2.通过btn1,test定位到具体的对象。
3. 点击button会触发click这个方法,从而去加载demo_test.txt文件替换原有内容
然后在本地静态页面测试,是ok的。可是在Django页面加载时候始终不能成功。找了半天原因才发现是路径加载原因,Django运行之后,无法通过相对路径加载jQuery,img,这些静态文件。这个error可以通过点击源码上的路径调试,如果文件是成功加载的,会直接打开文件,否则会出现如下类似错误。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmsAAABnCAIAAADQc5LhAAAYUklEQVR4nO2dv3KjStPGuQtdwgkJuAhHvgQS3cKW801JSJycyKFiEgJnW/WmCrbK1FdF1QmUWamzzfoLWmqa+SdmBBLyPr9yIMMwNDPDPPT0wGQEAAAAgHiyexsAAAAAPCRQUAAAACAFKCgAAACQAhQUAAAASAEKCgAAAKQABQUAAABSgILOybFrN9mm7Y51WRRlffd8AAAALMc8Cvoz+7n/dz9LVg9NXRabfNt17SbL6qa/ez4AAACWI9vv9/v9/qM/RB7YP+XbI9H7j/ef2c95FVTy/Jn9fP/xPuWQqqqyM1VVzWVJFMeuzfPtkagui2tkz5PPcZs/QUsBAGA9sA/6p//43R8+Jx/VF1khvfnuebeED8o6OlFBGdbReynoW1UdiY5d+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DAP5zu4DXnNebH6rm4uvHo80pjSFNQ40sOgev1A/kEa+QWCkpKRH1zcUU12Yn0RTT1XpmLqzdK/rLXRk/UFMmUnt2YLCrJfHNx7akrTJo/anyuxdBRmcFovE0vH7Wxw2C+z9PILv22g+/zNLEEikLvMoTQVyP2gXrqrPNc12Q4HeNjQDSebur7JpGd3v5Gj+/LBtecVypaviWkJ37LLi2uhjGx3yQS2sp8hcZ3vR4gn2Cl4Lu4AKyaK8YyAADLAgUFYKXowXYAwAqBggIAAAAp/D9XVFdrWio1hAAAAABJRU5ErkJggg==" alt="" />
一时想不到Django的解决方法,因为现在已经比较清楚Apache的静态文件的加载。直接部署到Apache上,用apache设置路径。成功加载!