How to make STATIC_URL work in external JS Files (Django) - python

I am trying to use STATIC_URL in external javascript file. I was expecting the result as it is working in template, but I found that it is not working in external Javascript file.
Please also tell me some work around to make STATIC_URL work in javascript file as it will make my project more manageable.
Also I am sending many ajax request and i want to have something like url template tag in my JS file.
Please let me know if you know solution of any of them.
Thanks

My solution to this problem is to pass STATIC_URL to your JavaScript code at some point so that it also knows the value. Something like this:
<script type="text/javascript">
myJavaScript('{{ STATIC_URL }}');
</script>
Your JavaScript function myJavaScript can then use it in URLs and wherever it is needed.

I use a simple Django app for making Python / Django settings available in javascript.
https://github.com/incuna/django-settingsjs
It works in the same way by rendering the settings in a template but provides a mechanism for making multiple variables available in JavaScript using a view, urls and optionally signals.

Related

Is there a way to add Div Block, Headers and other elements Django/Python's static folder?

Is there a way to add Div Block, Headers and other elements Django/Python's static folder?
I thought this would be simple, just use the same general formatting as I did with the stylesheet from the Django tutorial but I couldn't get it to work. Not sure if I'm on the right path of the most optimal way to set it up.
<title>"{% static 'polls/blah.txt' %}" /> </title>
Basically I am trying to make it so that all of my html pages can be altered by changing only one file rather than going into each page and changing the title or other text that might appear on each page.
This is called "Template Inheritance" and is covered very thoroughly in the docs.
To achieve this, you can either set up a base template with blocks that can be overridden in child templates that extend the base. Or you can create template "partials" and include them in your other templates.

With bottle.py, how do I properly render static file requests of templates?

I am experimenting with bottle.py, and I'm coming across some problems with requesting static templates vs rendering them.
I have some basic routes:
#route("/feed")
def show_feed():
# query database, calculate things, etc.
# code to show feed (which is dynamic)
#route("/submit")
def show_submit():
# query database, calculate things, etc.
# code to show submit
#route("/<filename:path>")
def serve_static(filename):
# code to simply return static files
I also have some templates:
views/submit.html
views/feed.html
There are no problems with the web server itself. It works as it is told. The problem is when I use links in my templates, as usual, to go from page to page:
Go back to the feed
When a user clicks on that link or manually enters ".../feed.html" or ".../submit.html", the URL .../feed.html is requested instead of /feed, and Bottle routes that to the serve_static(filename) function. As a result, the template is not rendered - instead, the static template is returned, complete with ugly things like "{{article[0]}}" and "% end % end".
How can I get Bottle to render these templates properly?
Is there a way for Bottle to know when to render templates when they are requested as static files? Is it considered an okay practice to change href attributes to what the server should expect? Is there something I'm not considering?
The problem is very simply that
<a href="feed.html">
has a wrong href given your URL pattern. Just change it to
<a href="feed">
It's also a good idea, as BrenBarn suggested, to move the templates into their own separate subdirectories, away from the one from which you want to serve truly static public files. However, per se, that wouldn't solve your problem -- you'd just get a 404 on clicks on the link in question. The core issue is fixing that <a>'s href!

Python : Can I do front end of django project using HTML / CSS?

Hi I am totally new to web development frameworks .I am currently learning python and django from the Django project tutorial..
My (rather dumb) question is when I am done with the small "poll" website will I be able to modify the front end of this project using HTML/CSS ?
Also, should I use tools like Dreamweaver to do that ? or are there other ways ?
Of course you will be able. That is the point of the web frameworks to let you build the whole application including frontend.
You'll get it all once you finish the tutorial. You'll see that Django has very nice templating language that allows you to build a html page according to data you get from your application.
<h1>{{ section.title }}</h1>
This result in normal HTML page in the end. And of course you can write any kind of CSS for those pages to get whatever look for your page you like.
Yes you can, you'll need to edit the templates in the location specified by TEMPLATE_DIRS. The templates are just html mixed with some python code to fill the content.

How can I use css files in django?

I'm using a template with block tags to include a css file.
Unsure on how to manage css files, I put this in my urls.py:
url(r'style.css$','portal.views.css')
with this in my views
def css(request):
return render_to_response('style.css')
When I use chrome's "inspect element" on any page that uses the template I can see all the css in the file, however nothing at works on the page itself. To test it I added
body
{
display:none;
}
But still no changes.
Do I need to render the response in a different way?
You shouldn't serve static files (like css files) that way.
Django provides a specific solution for that, make sure you read the complete and comprehensive documentation on that.

Pylons: Routes information availability from templates

I'm building a Pylons application using evoque as our templating engine, though I think my question is relevant to other template engines. I have a base template that I'm using for our pages, and that base template does all the includes for CSS and Javascript files. I'd like to perform conditional test to include/exclude CSS and Javascript files based on the actual page being display. Is there a way to access the routes information from the template, in other words to get the /{controller}/{action} information? This would allow me to get only the relevant CSS and Javascript files for that page based on the controller/action combination.
Thanks in advance,
Doug
You can pull the controller and action information from environ['pylons.routes_dict']['controller'] and ['action'].
I'm not sure if environ is passed into the tmpl_context by default, but if not, you can just add something like this to the BaseController.__before__ method:
c.routes_dict = environ['pylons.routes_dict']
Then reference c.routes_dict['controller'] in your template.

Categories

Resources