1

I apologize if this has been asked already but I have been trying for awhile and cannot figure out why when the route is changed in my django app all the css and js files that are used across the app like angular and jQuery need to be to loaded again.

paths

mysite
   settings,py
   urls.py
   views.py
static
   media
   static_dirs
   static_root
templates
   home.html
   partials
       dealDetals.html

settings.py

TEMPLATE_DIRS = (
  os.path.join(BASE_DIR, 'templates'),
  #BASE_DIR + "/templates/"
)

STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root')

STATICFILES_DIRS = (

  os.path.join(BASE_DIR, 'static', 'static_dirs'),

)

MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')

MEDIA_URL = "/media/"

STATIC_URL = '/static/'

urls.py

 urlpatterns = patterns('',
    url(r'^$', 'mySite.views.home', name='home'),
    url(r'^dealsDetails/', 'mySite.views.dealsDetails', name='dealsDetails'),

views.py

def home(request):
  template = "home.html"
  return render(request, template, context)


def dealsDetails(request):
    context = {}
    template = "partials/dealDetails.html"
    return render(request, template, context)

home.html

{% load staticfiles %}

// this loads correctly and can be seen in the network tab in chrome dev tools
<script src="{% static 'js/bootstrap.min.js' %}"></script>

<a href="/dealsDetails">deal details</a>

once the route goes to http://127.0.0.1:8000/dealsDetails/ all static files are no longer loading in the network tab in chrome dev tools.

The question is how do I share css and js files across different templates ?

I am able to load the js and css in one template but not more then one.

Do I need to add the script and link tags to dealsDetails.html too ?? I hope not :(

1 Answer 1

2

You have to include js and css files on dealDetails.html too.

To avoid repeating the same code, you can create one base.html template where you include css and js files, and then extend that base template in both your templates.

Take a look at documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.