Load jQuery into Django

前端 未结 5 866
后悔当初
后悔当初 2021-01-14 05:56

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 th

相关标签:
5条回答
  • 2021-01-14 06:21

    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.

    0 讨论(0)
  • 2021-01-14 06:25

    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>
    
    0 讨论(0)
  • 2021-01-14 06:34

    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">
    
    0 讨论(0)
  • 2021-01-14 06:34

    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.

    0 讨论(0)
  • 2021-01-14 06:35

    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.

    0 讨论(0)
提交回复
热议问题