Where should I save static resources for my Django project? - html

I'm designing a website for a nonprofit i'm part of using Python/Django.
The format is this: Urls point to Views which point to HTML Templates.
I can't for the life of me figure out where to store the images and stylesheets the HTML needs. Static? Media? Next to the templates? Next to the views? Nothing works.
I'm trying to use img src='image.jpg' and link rel='stylesheet' href='base.css' like I was taught, but it isn't working.
I'd stop development and study up on how files work, but I have a deadline.

See part 6 of the tutorial:
stylesheet (polls/static/polls/style.css):
and
Next, add the following at the top of
polls/templates/polls/index.html:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
The {% static %} template tag generates the absolute URL of static
files.
That’s all you need to do for development.
I'm trying to use img href='image.jpg'
It's <img src="..." alt="...">

Related

How can I include css files based on frontmatter in blogposts?

I have the following code in my skeleton layout.
{% if page.customcss%}
<link rel="stylesheet" href="{{page.customcss}}">
{% endif %}
And in pages I mention the customcss in the frontmatter like this:
---
layout: skeleton
customcss:
- 'assets/css/something.css'
---
This works for normal pages but for blog posts, jekyll generates then in a different directory and can't access the css and js files:
_site
-2021/1/1
-blog.html
-assets
-css
-something.css
-js
-x.js
for js files I've solved it with %link in the skeleton.html layout file:
<script src="{% link /assets/js/mirai.js %}"></script>
But it doesn't work for css files:
<link rel="stylesheet" href="{% link {{page.customcss}} %}">
`render': Could not find document 'assets/css/something.css' in tag
'link'. (ArgumentError) Make sure the document exists and the path is
correct.
I assume jekyll doesn't like the nested {{}}.
Is there a way to keep page.customcss while allowing usage in blogposts? I do not want to make a separate skeleton.html for blog posts.
Or am I doing something wrong here?
I found this within literal minutes. Keeping the ques up if it helps somebody:
Jekyll doesn't apply CSS to posts on GitHub Pages
basically, paths are not relative if is starts with /.
Ex:/assets/css/something.css
No need for %url.

My CSS File is not applying when I link it into the HTML

I have a CSS file style.css stored in a CSS folder and tried using <link rel="stylesheet" type="text/css" href="style.css"> to link it into the HTML file. I got a 404 not found error from this and am not sure what I am doing wrong.
Below is the code (there is not much at the moment):
{% extends "base.html" %}
{% block body %}
<div class="MainBody">
<link rel="stylesheet" type="text/css" href="style.css">
<div class="w3-container w3-padding-32 w3-hide-small center-align">
<h1>About Us</h1>
</div>
</div>
{% endblock %}
This shows the full path of style.css within the project
Please let me know what is being done incorrectly.
Actually, you answered your own question:
I have a CSS file style.css stored in a CSS folder
Your href attribute should look something like: href="/css/style.css"
The href should should have the path where the file is located along the file name not only the file name.
Example :
<link rel="stylesheet" type="text/css" href="./css/style.css">
we use "./" to search in the current folder.
we use "../" If you want to come back from the current folder.
Try this:
In the href, would you not leave the first / off? I thought that would go to a root folder. Try and eliminate that first slash and see what happens. As was said, the 404 is because it's looking in the wrong place.
I see this is a structure of a Flask app. In Flask, the CSS files and other assets should by default be stored in the static directory which should be directly inside your home directory.
Your file structure should look like this:
TSAWeb_App
|__ templates
|__ index.html
|__ static
|__ CSS
|__ style.css
Then you to access it inside the web app, first import url_for method
from flask import Flask, url_for
Then you can refer to CSS file as:
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='CSS/style.css') }}">
Then when the app is run, the href tag will have the correct value set
UPDATE: If you're using Django, see this doc
Configuring static files
Make sure that django.contrib.staticfiles is included in your
INSTALLED_APPS.
In your settings file, define STATIC_URL, for example:
STATIC_URL = '/static/'
In your templates, use the static template tag
to build the URL for the given relative path using the configured
STATICFILES_STORAGE.
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image">
Store your static files in a folder called static in your app.
For example my_app/static/my_app/example.jpg.

django replace html href and src to a {% static %} tag

how I can django replace html href and src to a static tag in Pycharm?
For example:
<link rel="stylesheet" href="libs/owlcarousel/assets/owl.carousel.min.css">
<img src="img/logo.png" alt="">
into
<link rel="stylesheet" href="{% static 'landing/libs/owlcarousel/assets/owl.carousel.min.css' %}">
<img src="{% static "landing/img/logo.png" %}" alt="">
thx!
You can find and replace into the project with Ctrl + Shift + R.
Into the file selection, you can specify that you want to perform replacements in *.html files, and that we want to search for a regex (check the checkbox Regular expression).
As pattern, you can write a pattern like:
\b(src|href)="([^"]*)"\b
and as patttern to replace:
$1="{% static '$2' %}"
You might want to consider tweaking the pattern a bit, for example to only replace paths that start with libs/, etc.
this will then propose the changes. I advise you to look through these manually since there can be false positives: proposed changes, that should not change.

CSS image path missing using assetic in symfony2

I'm trying to follow Symfony best practices with keeping general assets stored directly in the /web root. I am trying to set a background image in css which is not loading.
Folder Structure.
web
assets
css
style.css
images
image.jpg
I'm including the style.css file via assetic. I've installed assets with assets:install web --symlink command. I've also tried the assetic:dump command.
#div {
background: url("../images/image.jpg");
}
{% stylesheets filter='cssrewrite'
'assets/css/style.css'
%}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}"/>
{% endstylesheets %}

assetic symfony2: html content inside css files

I'm trying to import css file inside twig file using assetic.
{% stylesheets 'bundles/sfeuser/css/bootstrap.css' filter='cssrewrite' %}
<link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
{% endstylesheets %}
when I load the page. it was only HTML without style
and when I checked the css file. I found out that it contains HTML content of the page loaded.
In the security.yml file I forgot to remove a firewall with authentification that was applied on the whole application.
That's why when the browser tries to access the CSS file it gets redirected to the login page. Therefore the HTML content of the login page was found in the CSS file.