rafaelcb21
rafaelcb21

Reputation: 13304

Having confusion with pathing in Django templates

When I use the url(r'^consultar/$', 'rcb.core.views.consultar'), in browser http://localhost:8000/consultar the consultar.html file find the jquery-1.11.2.min.js file, appearing the following information:

Remote Address:127.0.0.1:8000
Request URL:http://localhost:8000/static/js/jquery-1.11.2.min.js
Request Method:GET
Status Code:200 OK

But when I use the url(r'^proposta/consultar/$', 'rcb.core.views.consultar'), in browser http://localhost:8000/proposta/consultar/ the consultar.html file not find the jquery-1.11.2.min file.js. Appearing the following error:

Remote Address:127.0.0.1:8000
Request URL:http://localhost:8000/proposta/static/js/jquery-1.11.2.min.js
Request Method:GET
Status Code:404 NOT FOUND

Could someone help me fix the code to run the url http://localhost:8000/proposta/consultar/

Below is the code and the structure of files and directories:

consultar.html

{% extends 'base.html' %}
....

base.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="../static/js/jquery-1.11.2.min.js" type="text/javascript"></script>
</head>
<body>
    {% block corpo %}{% endblock %}
</body>
</html>

views.py

def consultar(request):
    propostas_salvas=search()
    return render_to_response('consultar.html', { 'propostas_salvas' :  propostas_salvas}, context_instance=RequestContext(request))

My structure of files and directories is:

rcb (Project)
    core (is the App)
        static
            js
                jquery-1.11.2.min.js
        template
            base.html
            consultar.html
        views.py
        ...
        ...
        ... 

Upvotes: 2

Views: 111

Answers (1)

GwynBleidD
GwynBleidD

Reputation: 20559

You should use path to javascript relative to your domain, not to document you're requesting, because for each document relative path to javascript would be different. So change that line:

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

into:

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

Or even better, load static in your template and use static file management built into django:

    <script src="{% static "js/jquery-1.11.2.min.js" %}" type="text/javascript"></script>

That way you can change later your STATIC_URL in settings and django will correct path to your static files. Even if they are on different domain (some CDN or no-cookie domain just for your static files).

Upvotes: 3

Related Questions