CherryPy configuration for CSS file access - python

The following is the result of CherryPy and css pathing problems I have recently posted, both of which have been answered, but another problem has arisen.
I have a html page which I preview in a browser (via. editor/IDE) and which calls a css file from a css folder in parallel with my application folder (containing main.py and My.html file). For this I use relative pathing in the html header...
<link rel="stylesheet" href="..\css\commoncss.css" type="text/css">
All good so far. However, when I run Main.py, the css file cannot be found, and the page looks a mess :-( The CP configuration file includes the following line...
tools.staticdir.root = "my\app\folder" # contains Main.py and My.html
but no other staticdir declarations because CP should be looking for ..\css\commoncss.css relative to the static root folder (am I right?)
I could have my CSS folder as a top-level folder (then I could use href="/css/commoncss" and declare /css as a staticdir), but that's ugly. Alternatively the CSS folder could be a subfolder of the app folder, but I really need the freedom to be able to put the .css file(s) in a different path if possible (could be common to more than one app.).
I really would like to crack this problem, because otherwise it means the html designer cannot use the same template as the Python programmer without changing the href directive.
Any help would be appreciated.
Alan

but no other staticdir declarations
because CP should be looking for
..\css\commoncss.css relative to the
static root folder (am I right?)
You can't reach into your physical file directory (static dir) via URLs, nor should you want to.
Cherrypy is looking for the css file relative to your HTML file in the URL hierarchy. If your HTML file is at root, then this won't work. If it's at, say: /stuff/blarg.html, then it would go down to the root and look for the css folder.
I think it's easier to just give an absolute path, because it's reasonable to stipulate that the css directory be in a known location: "/css/commoncss.css"

Related

Flask can't find local static files even though they exist and the specified path is correct

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') }}"

Implementing HTML & CSS into Django

I'm trying to make my views.py point to a HTML page I've made that has embedded CSS, is this the best approach? I'm also running Django locally for testing purposes until it is moved to a production server how would I make local links to point to my HTML ?
https://docs.djangoproject.com/en/1.10/ref/settings/#templates
The default includes app/templates/ to your template paths and i would recommend to store your template files there.
Static files like .css and .js are served in similiar way
https://docs.djangoproject.com/en/1.10/howto/static-files/
The django test server also serves static files like css and js for you.
I would also recommend to move your css in one or more seperate .css files.
It helps you on the long run to keep your project clean.
1) In your views.py should be something like:
def my_view(request):
return render(request, 'my_html_page.html', {})
2) You should put 'my_html_page.html' to folder "templates/"
3) You should make folder "static" and put there you css file (i.e. styles.css)
4) in "my_html_page.html" you should link your css like this:
<link rel="stylesheet" href="***{% static 'styles.css' %}***" rel="stylesheet">
5) you should run command
python manage.py collectstatic
p.s. on production server you should install whitehoise
pip instal whitenoise

Retrieving file from Heroku

I know it's been asked, but I have a Python script that generates a file in Heroku. I'm on the Cedar stack, which apparently now allows you to write anywhere within /app and not just /tmp.
Basically, I run a script and the file that I'm interested in is dropped in /app, which if I'm not mistaken is some kind of temporary directory used by my Heroku app. I'll use /app/test.txt as an example of the file that's generated by my script that I'm interested in making a link to.
Interestingly, when I do an ls within the Python script (through os.subprocess or something similar), I get a directory listing of everything in /app, including requirements.txt, Procfile, /templates/ and so on, including the test.txt file I'm interested in.
So, my Python script looks like this:
#app.route('/', methods=['POST'])
def my_form_post():
return render_template("my_form_2.html", filename = "test.txt")
and the corresponding template my_form_2.html looks like this:
<!DOCTYPE html>
<html lang="en">
<body>
<a href={{filename}}>Right-click to download!</a>
</body>
</html>
The key with that html href up above is that I want it to point to a URL, like my-app.herokuapp.com/app/test.txt. The HTML above produces the preceding link, which unfortunately isn't valid. Essentially, I'm trying to get the absolute path to the file I create with my Python script. Any ideas on how that can be done?
Before anyone mentions S3, I'm trying to avoid having to use it for now, since it's a file that's small and doesn't need to be transferred to some other servers.

Django serving static pages from Sphinx

I have a large group of static html pages that I've generated from sphinx, and I'd like to show them on my Django site. I can connect to at least one page by putting the html/ folder form sphinx in a templates directory inside my app, and changing the urls.py file to include
url(r'^$', TemplateView.as_view(template_name="index.html")
Then it finds the index.html file inside myapp/templates/html. However, none of the internal links work (it'll try to redirect through Django and give me a 404 error). Also, the static files won't load in (Sphinx generates a _static folder, and even though I put that in the myapp directory, and the Chrome network tab tells me it's trying to load the css from myapp/_static, still nothing).
Is there any way to make all the links relative to each other inside this project? Alternatively, can I get Django to just serve up the whole project as static pages?
It looks like FlatPages is almost what I'm looking for, but I have more than just a title and content in these pages.
Firstly, you should not be using Django to serve essentially static files. This is what your webserver is for.
If you are using Apache, you can use the <directory> directive to serve these files alongside your Django routes.

django compressor and clevercss with absolute url paths

When using django, compressor, and clevercss, I set my css url to an absolute path. Clevercss is then passed the path of the .ccss file without the COMPRESS_ROOT prefixed (the absolute path). When I set my css url to a relative path, clevercss processes the ccss files, but the browser then correctly looks for relatively placed css files (e.g. mywebsite.com/profile/user/1/css/stylesheet.css)
Compressor, however, does use the MEDIA_ROOT when the css link is a relative url, but not when an absolute url is used. This has the unfortunate effect of my css either being rendered by clevercss and not accessible by the browser (unless on the home page), or clevercss not having access to the files (due to an absolute url being used). Ironically, the examples offered on http://github.com/mintchaos/django_compressor use absolute urls for the css paths.
I think I'm doing something wrong here, but I'm not sure where it could be and have spent quite a few hours looking. I'm also currently running this locally through ./manage.py runserver and serving some static files (images) through django. (this is fine for my local development).
I can't speak to django-compressor specifically; but I have been dealing with finding a good automatic compression solution for the CSS and JS files of my Django-powered web applications. I'm currently using django-static. It's been really easy to set up and use, IMO. I was running into some issues running django-compress (different from django-compressor) when I decided to give django-static a try. So far it's been great. Might be worth checking out. It can be found here: http://github.com/peterbe/django-static.

Categories