开发者

Problem with loading Javascript files in a Django/Ext JS 4 based application

I'm trying to get this Extjs4 guide working with Django 1.3. I managed to get it working without Django with just a static json file as the data store.

Then I took the following approach to make it work with a Django app.

  • First I created a Django app named userdb with a simple User model(only a username and an email field).
  • Then I created two views in Django First one(mysite.com/users/) is a TemplateView which just directs to the index.html template file. The second one(mysite.com/users/getusers) produces a json response exactly like one in users.json file in the guide.
  • I have a directory project-static in Django project directory where I put all my static files. So I created project-static/userdb/extjs/ and put all the sdk files there. project-static/userdb also contains all the 开发者_开发问答JS files I created for the Ext JS app in the guide.
  • Then I modified the Users.js in app/store/ to use the url of the getusers view.
  • After that I collected all the static resources to a static folder using python manage.py collectstatic to be served via mysite.com/static/.

When I visit mysite.com/users/ all the JS files of Ext JS loads fine, But the JS files I wrote for the app doesn't load because Ext JS does not load them from mysite.com/static/ instead it uses mysite.com/users/. What can I do to resolve this issue?


I realized while reading this blog post that the problem was with the loader not using the /static path of my application. So i needed to do the following to resolve the issue,

in index.html,

    <html>

      {% load static %}
      <head>
        <title>Account Manager</title>

        <link rel="stylesheet" type="text/css" href="{% get_static_prefix %}userdb/extjs/resources/css/ext-all.css">
        <script type="text/javascript" src="{% get_static_prefix %}userdb/extjs/ext-debug.js"></script>
        <script type="text/javascript" src="{% get_static_prefix %}userdb/app.js"></script>

        <!-- This is Required before the loader starts, so we have to manually include it -->
        <script type="text/javascript" src="{% get_static_prefix %}userdb/app/controller/Users.js"></script>
      </head>
      <body></body>
    </html>

Then in app.js at the begining of the file,

Ext.Loader.setPath('AM', '/static/userdb/app');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜