Download jQuery in Django

I have a really basic question. I am trying to create some AJAX functions in a Django project. I plan to use jQuery. Right now, I'm just running the code locally through Linux. I tested the code here , so I'm sure it works. But it's hard for me to figure out where to place the jQuery source code in combination with my settings.

I downloaded jQuery and put it in what I think is my Media folder. The settings.py file is as follows:

MEDIA_ROOT = os.path.join(PROJECT_DIR, 'books/media/')<br>
MEDIA_URL = 'http://localhost:8000/books/media/'

In my html template, I refer to:

<script type="text/javascript" src="/media/js/jquery-1.4.2.min.js">

But none of my features work. I am sure that this is something stupid and obvious (although obviously not obvious to me). How to configure jQuery?

+3
source share
5 answers

You can hardlink the link as

<script type="text/javascript" src="/books/media/js/jquery-1.4.2.min.js">

or if you create your template with RequestContextand use a context processor django.core.context_processors.media, you can access MEDIA_URLin your template.

<script type="text/javascript" src="{{ MEDIA_URL }}}js/jquery-1.4.2.min.js">
+6
source

I think you will need src="/books/media/js/jquery-1.4.2.min.js"

Also, I do it like this.

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("jquery", "1.4");
</script>
+3
source

Django ( HTTP-). url.py:

(r'^mymedia/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),

, mymedia, -.

+2

:

  • URL http://localhost:8000/books/media/js/jquery-1.4.2.min.js curl , .

  • Check the URL configuration to see that you have a URL designed to serve the media using the static service.

+1
source

Step 1: put your jquery-2.0.3.js in the APP subfolder "static", for example, "books / static / jquery-2.0.3.js";

Step 2: configure your urls.py, add the following line:

r('^static/(?P<path>.*)$','django.views.static.serve'),

Step 3: in the template file, use the js file as follows:

<script type="text/javascript" src="/static/jquery-2.0.3.js"></script>

That's all.

0
source

Source: https://habr.com/ru/post/1764050/


All Articles