Javascript from external file not loading

我与影子孤独终老i 提交于 2019-12-06 13:40:11

From the tags and your template code, I gather that you are using Django. To understand your issue, you'll have to understand how Django views work relative to your browser, and what happens when your browser issues a request for a given url.

What happens on the Django side:

What happens when you request an url is that your base urls.py file will be searched for pattern matching your url. If a pattern is encountered, then the corresponding view will be called.
The viewwill carry out its logic, and will use a template to render its response into html.

What happens from your browser's point of view

The browser requested an url, and received a response, it is not aware of the fact that a view was called, and that it fetched a template somewhere.

What this means to you

The fact that your search.js file is located next to your template is totally irrelevant, as your browser never requested any file from this directory, it's the view that did, when it fetched its template.

Actually, your browser's request for search.js will be forwarded to Django by your webserver and will (most likely) result in a 404 error, unless search.js resolves to a view in your urls.py.

How you can use this to solve your issue

You'll need to serve your search.js file from a directory that can be accessed by the browser. Usually, this is done in three steps:

  1. Configure your webserver so that it serves any path starting with /static/ on its own (somehow, this means not forwarding the request to Django).
    In Apache, you'd use the following rule: Alias /static/ /YOUR/STATIC/DIRECTORY/

  2. Configure Django's settings.py to use /YOUR/STATIC/DIR as STATIC_ROOT, and /static/ as STATIC_URL.

  3. Put your search.js file in your /YOUR/STATIC/DIR/

  4. Use src="/static/search.js to reference your file in your html template.

One last thing: if you're using the development server, you might need to ensure your STATIC_URL starts with the full path to your server, including the port. (Or you might have issues with browser security policies).

A few additions:

Your should be using template tags so that you don't have to write /static/ in your template.
You should be using manage.py collectstatic to autimatically put static files in your static directory.

Most importantly, you should investigate what Django's MVC (or MTV) model is about.

Paths prefixed with / points to the root - not the current folder. In this case you want to drop the / prefix.

Your HTML files at project/ps/templates/bill.html which links a JS script /search.js means it's looking for the files in the completely wrong location.

For instance, you can see how it resolves the path if you add a link to /search.js in your HTML.

For illustrative purposes, create an HTML file on your desktop:

<html>
<body>

<a href="/foobar.txt">Hello</a>
<a href="foobar.txt">World</a>

</body>
</html>

When you hover over the link you will see that Hello it resolves to file:///foobar.txt and World resolves to the path to your desktop - in my case file:///C:/Users/thm.ARC/Desktop/foobar.txt.

I had to make a mix of your answers in order to fix the problem but eventually it was fixed. I had to make the following modifications:

  • Add the '/static/' folder to STATIC_URL and STATICFILES_DIRS in the settings.py file
  • Import the .js files (search.js and the jQuery source) with the following commands, in this order:

    <script type="text/javascript" src="/static/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="/static/search.js"></script>
    
  • And make the according file structure, placing the "static" file in the project's root directory.

    i.e. Psroot:

  • psApp
  • static
  • templates
  • and the rest of the files (settings.py, urls.py, manage.py, etc.)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!