I'm hosting a Pylons application using Apache as a proxy to paste, which serves my app. I'm trying to set up a Rewrite Rule to force browsers to update modified css, js, and image files, as inspired by the latter half of section 10.8 in Chapter 10: Survive the Deep End. It works by writing a view helper to automatically include the modification time of any of these files in the url, so the html may look like this:
<link href="/css/main.1302802028.css" media="all" rel="stylesheet" type="text/css" />
Then, you use Rewrite Rules to get the server to forward the request to the location of the actual file at /css/main.css. That way, anytime you update your file the client's browser thinks it's getting a new file, thus ignoring its cache.
I tried setting it up in my site's conf file within apache with a rewrite rule like so:
RewriteEngine On
RewriteRule ^(.*)(css|img|js)/(.+)\.(\d+)\.(css|js|jpg|gif|png)$ $1$2/$3.$5 [L]
However, the Rewrites don't seem to function since I'm using Apache with ProxyPass. Is there either a way to get them to work with a proxy, or use paste/pylons to achieve the same effect?
Thanks!
I don't know if you are running on top of Linux or not, but have you simply tried setting up a symlink?
Related
I use render_template in conjunction with Flask to render an html file, index.html, which in turn references a CSS stylesheet, "templatemo-style.css". Within that css file, references are made to other local files like images and so on.
The folder structure looks like this:
/PARENT FOLDER
/templates
/index.html
/static
/assets
/images
/various files
Within the "assets" folder, there is sub folder called "css" which houses the css file.
The css file makes a call to the following url to grab the website's banner image:
background-image: url(../assets/images/main-banner.jpg);
The problem is, when I RUN the flask application, I get the following 404 error in my terminal:
127.0.0.1 - - [02/Aug/2021 13:13:51] "GET /static/images/main-banner.jpg HTTP/1.1" 404 -
For some reason, the application isn't calling the path that I specified in the code. I've searched all over the code in various different files for the place where this mysterious call is made and I can't find it anywhere. Even if I comment out my "background-image:" line above, I still get the same error. I should note that for the front-end i'm using a css/bootstrap template that I found online and then modified to my liking. I've searched the js code as well and that call isn't made there either.
I'm completely stumped and have no idea where this mystery call is coming from. Anybody have any possible ideas? Thanks
EDIT: figured it out. Its because my browser was caching my css file and therefore not running updated code.
Maybe this would work
background-image: src="{{ url_for('static', filename='filename') }}"
So the problem I'm having is that my web app only sometimes loads the css file and it uses the same css file data that was loaded even if I make edits to the css file and even when I delete the file. I have no clue what's going on. I have noticed that when it does load the css correctly the following message is displayed:
127.0.0.1 - - [08/Jun/2015 14:46:19] "GET /static/style.css HTTP/1.1" 200
My style.css file is under a folder named static and in my html file I have
<link type='text/css' href='{{ url_for('static', filename='style.css') }}' rel='stylesheet'>
Flask sets cache control headers on static resources, and your browser will cache those files up to 12 hours.
Your options are:
Set the SEND_FILE_MAX_AGE_DEFAULT configuration directive to a shorter value; it is set to 43200 but you can set it to 0 if you really want to.
Add a cache buster query parameter to your static URLs; if your static URLs end with ?<somerandomvalue> your browser will reload the resource if that random value ever changes. You could do this by hand each time or you can use this Flask snippet to override the url_for() function to add such a value for you based on the file modification time.
Use an incognito browser window (private browsing mode in other browsers) to force your browser to ignore the cache for a specific session. Each time you open a new incognito tab or window, the cache should be invalidated for that session.
Please use ctrl + f5
because when you use simple refresh it shows data from cache but when you do ctrl + f5 it render new data
I guess it's because of the fact that your browser catches CSS files. In fact, there is no way for browser to detect your changes unless you refresh your page manually by pressing Ctrl+F5 and force browser to flush the resources.
Update 1:
The only One way to fix this is to add a randomly generated suffix to your CSS/JavaScript files and change those values whenever you make a change to your files. This way, the browser catches the latest CSS file and ignores the previous files.
Ctrl+Shift+R to refresh the page works well.
I am using flask and Jinja templates. None of my CSS is being applied, except for bootstrap which is being downloaded from an external host.
Here is the line in my base template for my own stylesheet:
<link rel=”stylesheet” type=”text/css” href="{{ url_for('static', filename='style/stylesheet.css') }}">
And then when I open the page in chrome I can follow the link, and it opens up the file successfully. However when I look in the frames I cannot see my stylesheet under the stylesheets section:
Here is the request from the server: GET /static/style/stylesheet.css HTTP/1.1" 200 -
It looks likes it's not being recognized as a css file ? I'm not great with web stuff so hopefully this is something simple.
I have no idea what was happening here. This issue has been ongoing for days.
To fix it I simply copied the line that loads the CSS, saved my project, pasted it back in, and ran the server. Mass confusion.
For anyone else still having issues with this I found this that suggests the CSS is not being "hard refreshed".
I fixed this issue on my Mac in Chrome by holding down both ⌘ Cmd+⇧ Shift and pressing R.
I would like to change the logo of a website based on which menu is currently activated/seen by the user browsing the website.
For instance I have www.urltowebsite.com/menu1 = Header Logo 1
And then I have www.urltowebsite.com/menu2 = Header Logo 2
And on top of this I want to add an else statement stating that: If any other menu is selected, use header logo 3.
How can I make this possible with Python? I cant seem to wrap my head around what to define where and how to call up the different functions on the HTML website.
Oh and I insist doing this with Python. And preferably without any framework such as Django. But if needs be I can install web.py
EDIT:
Am I forced to go with php then? I would like to once and for all start utilizing Python on my web projects.
The website is made in simple HTML as I said first. The Javascript functions are only used to serve the HTML menu's through AJAX. Again this does not matter much for what I am trying to do, as menu's have classes and I can define those in php and thus change my logo/header.
What I want to do is to use Python in this instance. Here is a code snippet from the site:
<div id="header">
<span class="title"><img src="http://www.url.com/subfolder/images/logo.png"/>
</span>
</div>
And some more relevant to this:
<div id="menu">
<ul>
<li>001</li>
<li>002</li>
<li>003</li>
<li>004</li>
<li>005</li>
<li>006</li>
<li>007</li>
<li>008</li>
</ul>
</div>
So can I use python here?
You're asking to do the wrong thing the wrong way.
In order to change the logo based on the URL in Python , you need Python to generate the page and know what that url is.
There are two ways to do that in Python:
Use an existing Web Framework
Write your own Web Framework
"Python" doesn't know or care what your URL is - the frameworks and support libraries ( Django, Pyramid, Bottle, Flash, Tornado, Twisted, etc) figure out what the URL is by an integration with an underlying web server ( though some have their own webserver coupled in ). Similarly, PHP doesn't really know or care what the URL is - that information comes from an integration with Apache or FCGI/Nginx/etc. PHP tends to ship with most/all of that integration done. It's also worth noting that PHP is not just a language, but a web framework. Python is just a language.
Most Python frameworks will be written to the WSGI spec and have a "request" object that has all the data you want ( and many use the WebOb librbary for that ).
If you plan on doing everything with static HTML files, then you have a few options:
have a single static directory. use javascript to figure out the addressbar location, and render the corresponding logo / write the headers & footers.
have a "template" directory of all your HTML. use a Python script build a static version of each website with the custom headers/footers and configure your webserver to serve a different one for each domain.
No, Python cannot run inside the HTML web page. If you're really serving plain HTML pages then you must use javascript to execute code in the browser once the page is loaded. However, since you mention using AJAX, it sounds like it's not really true that you're serving plain HTML but rather have some server side code. If so, that server side code is the place to put your HTML-construction logic. To know the best way to do that, you would have to describe what's happening on the server.
Although I haven't used it, I have heard that the pyhp project more or less provides php-like embedded functionality for python.
I tried profiling my web application and one of the bottlenecks reported was the lack of gzip compression. I proceeded to install the gzip middleware in Django and got a bit of a boost but a new report shows that it is only gzipping the HTML files i.e. any content processed by Django. Is there a way I could kludge/hack/force/make the middleware gzip my CSS and my JS as well?
Could someone please answer my questions below. I've gotten a little lost with this.
I might have gotten it wrong but
people do gzip the CSS and the JS,
don't they?
Does Django not compress
JS and CSS for some browser
compatibility issues?
Is compressing
and minifying the same thing?
Thanks.
Your CSS and JS should not be going through Django on your production system. You need to configure Apache (or Nginx, or whatever) to serve these, and when you do so you'll be able to set up gzip compression there, rather than in Django.
And no, compressing and minifying are not the same thing. GZip compression is done dynamically by the server when it serves your request, and the browser transparently unzips the file when it receives it. Minification is the process of removing comments and white-space from the files, and sometimes concatenating multiple files into one (ie one css and one javascript, instead of lots of each). This is done when you deploy your files to the server - by django-compress, as Ashok suggests, or by something external like the YUI compressor, and the browser doesn't attempt to reconstruct the original file - that would be impossible, and unnecessary.
You should think about placing your django application behind an HTTP reverse proxy.
You can configure apache to act as a reverse proxy to your django application, although a number of people seem to prefer using nginx or lighttpd for this scenario.
An HTTP reverse proxy basically is a proxy set up directly in front of your web application. Browsers make requests from the reverse proxy, and the reverse proxy forwards the requests to the web application. The reverse proxy can also do a number of interesting things like handle ssl, handle gzip-compressing all responses, and handle serving static files.
Thanks everyone.
It seems that the GzipMiddleware in Django DOES compress CSS and JS.
I was using Google's Page Speed plugin for Firebug to profile my page and it seems that it was generating reports based on old copies (non-gzipped versions) of the CSSs and JSs in my local cache. These copies were there from the time before I enabled the Gzip middleware. I flushed the cache and it seems that the reports showed different results altogether.
Follow Daniel Roseman's suggestion, "Your CSS and JS should not be going through Django on your production system"
If you want to serve through Django then
you can compress css, js files using django-compressor, django-compress