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.
Related
I have made a flask application which generates .pdf from html with weasyprint and send it as attachment. But apparently bootstrap 4 css is not applied. I can't find a solution.
It is working well with pdfkit but I need weasyprint, since pythonanywhere.com does not support pdfkit.
I have tried linking bootstrap, using bootstrap css as file in my html, but there was no difference.
This is my python part, which generates and sends pdf.
#app.route('/pdf_send', methods=('GET', 'POST'))
#login_required
def pdf_send():
rendered = render_template(
'pdf_send2.html',
name=name_g,
surname=surname_g,
email=email_g,
address=address_g,
invoice_no=invoice_no_g,
dict_g=dict_g,
bendra_suma_g = ('%.2f' % round(float(bendra_suma_g[0]), 2)),
send=send_g,
today=today_g
)
css_file = ('static/bootstrap.css')
filename = 'SF-' + invoice_no_g +'.pdf'
html = HTML(string=rendered)
css = CSS(filename=css_file)
html.write_pdf(filename, stylesheets=[css])
send_email(filename, email_g)
return redirect(url_for('index'))
I know this is an old thread but I stumbled over it while looking for an answer to the same question, so I'm adding my workaround in case it helps anyone else. I'm going to start by pointing out that although this does work Bootstrap really isn't intended to be used for printing, and weasyprint ignores anything inside a #media query so you're going to need to add a lot of css anyway in order to make it render correctly so it's probably not worth it.
Using the code from the original question, if you add the bootstrap cdn css to the stylesheets list it will render:
html = HTML(string=rendered)
css = CSS(filename=css_file)
html.write_pdf(filename, stylesheets=[css, "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"])
However, given the issues with Bootstrap rendering, I found this trick to be considerably more useful for applying fonts.
weasyprint doesnt work well with bootstrap .
This is the reference Link to Github site for clarity.
Whatever bootstrap classes you write will be ignored by WeasyPrint.
You can see the "Ignored ..." messsages in WeasyPrint error log(if you ever initilised).
So its better to use CSS for the print-pages
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.
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!
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.
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.