3

I have a really basic question. I'm trying to build some AJAX functionality into a Django project. I plan to use jQuery. Right now, I'm just running the code locally through Linux. I've been testing some code here so I'm reasonably certain that it works. But I'm having trouble figuring where to put the jQuery source code in combination with my settings.

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

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

In my html template, I'm referencing:

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

But none of my functions are working. I'm sure it's something stupid and obvious (though clearly not obvious to me). How do I correctly source jQuery?

Ankur
  • 5,086
  • 19
  • 37
  • 62
Ed.
  • 4,439
  • 10
  • 60
  • 78

5 Answers5

7

You can hardcode the link as

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

or if you render your template with a RequestContext and use the django.core.context_processors.media context processor, then you can access MEDIA_URL in your template.

<script type="text/javascript" src="{{ MEDIA_URL }}}js/jquery-1.4.2.min.js">
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • awesome! Any idea why this works: but this doesn't: – Ed. Sep 09 '10 at 20:20
  • 3
    You can't have src and code in the script tag at the same time. It's just the way html is specified. See this stackoverflow question: http://stackoverflow.com/questions/1056325/javascript-inline-script-with-src-attribute – Alasdair Sep 09 '10 at 20:38
3

I believe you'll need src="/books/media/js/jquery-1.4.2.min.js"

Also, though, I do it thusly.

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("jquery", "1.4");
</script>
Dave Aaron Smith
  • 4,517
  • 32
  • 37
  • Is it preferable to load it like this or to download it to my own directory? – Ed. Sep 09 '10 at 20:00
  • I prefer doing it this way because there's 1 less Javascript file to worry about and I use google.load for a few other things as well such as their maps API. Really though, there's nothing wrong with downloading it into your own directory if you prefer. – Dave Aaron Smith Sep 09 '10 at 20:46
  • In theory it can make your website faster. If a user goes to website A that uses Google to host jQuery, then visits your website, then jQuery is already in the browser cache, so doesn't need to be downloaded again. – Alasdair Sep 09 '10 at 20:46
2

You need to setup Django to serve your media files (otherwise, serve them from a proper HTTP server). Add the following line to your url.py:

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

Make sure you change the mymedia part to align with your media directory.

Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
  • what does it mean: to serve static media? And is this only necessary from a local project? – Ed. Sep 09 '10 at 20:23
1

Can you try the following:

  1. Access the URL http://localhost:8000/books/media/js/jquery-1.4.2.min.js using your browser or curl and see what turns up.

  2. Check your URL configuration to see that you have an URL defined for serving media using static serve.

Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141
0

Step1: put your jquery-2.0.3.js in the APP's "static" subfolder,like "books/static/jquery-2.0.3.js";

Step2: config your urls.py, add the following line:

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

Step3: in your template file, use the js file as follows:

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

That is all.

brown
  • 1
  • 1