0

I have a CSS file which is being used in my template file

<link href="{% static "assets/style/reset.css" %}" rel="stylesheet" type="text/css"/>

I want to access the 'static' in my JavaScript. Is it possible? How can I do it?

1 Answer 1

0

Your javascript has to pass through django's template redering. You could do this by rendering your javacript inside of your template like Django Template Variables and Javascript suggests.


Or you could create a view that just renders your javascript dynamically:

(r'^dynamic.js$', dynamic_js)


def dynamic_js(request):
   """
   Create a js file dynamically      
   """   
   return render(request, 'dynamic.js', 
                 content_type='application/javascript')


# dynamic.js    
# can use template tags now!

Having django serve your static assests is extremely inneffiecient, and is one of the first things to migrate away from if you want a speedy site. If your js file is rarely changing it might be better to create some sort of build process where you can generate your dynamic js with django (so you can use the template variables) and save it to you static folder, so that your webserver can serve the content for you!

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

1 Comment

See also another related question and anwers here stackoverflow.com/questions/9576675/…

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.