point to a custum setting in the template - css

I want to create settings in the settings.py file that i can access in the templates that i use without passing the setting in ,in the views.py using django
settings.py
CSS_FOLDER_ROOT = "/home/brian/Projects/RaffleThis/RaffleGym/stylesheets"
CSS_FOLDER_URL = SITE_DOMAIN + "/CSS/"
I want the server to serve the files from CSS_FOLDER_ROOT when HttpRequest is sent
working on the django template .html file and the views.py file

I guess one way to do this is by creating a context processor.
Create a context_processors.py somewhere in your project
import settings
def css_url(request):
return {'CSS_URL': settings.CSS_URL}
Add the context processor in your settings
CSS_FOLDER_ROOT = "/home/brian/Projects/RaffleThis/RaffleGym/stylesheets/"
CSS_URL = '/css/'
TEMPLATE_CONTEXT_PROCESSORS += (
"django_app.context_processors.css_url",
)
Then you can use something like this in your templates.
<link rel="stylesheet" href="{{ CSS_URL }}<filename.css>" />

Related

Static files apply on all pages except index.html and base.html

I have an issue with static files. I uploaded the code on pythonanywhere, it runs, and I correctly linked the static URLs in pyanywhere>web page. the problem is that my static does not load on the index and base.html but on the other pages (where I have base.html extend it applies). Can you give me some support, please?
how it looks
python anywhere url
STATIC_URL = 'static/'
STATIC_ROOT = "home/Tsh2Dns/businessplaner/static/"
#i tried to give root from pythonanywhere. still same response
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),
]
STATICFILES_DIR = [
os.path.join(str(BASE_DIR.joinpath('static')),)
]
folders:
here is the base
base
enter image description here
landing page.
all the other pages have the same beginning with base extend.
I have to mention that on my local it works just fine, only on pythonanywhere this happen

Serving static files in an HTTP server

I'm using golang as the backend for a medium-sized web app which has a few pages and a lot of CSS and javascript in different folders, I'm trying to serve the website using golang but only the index file loads , the other pages, javascript, CSS doest load. since my HTML files, all are different from each other I'm not using templates
here is the file structure
-static
-assets
-css(folder with subfolders)
-js(folder with subfolders)
-pages (folder with differnt html pages)
-signup.html
-dashboard.html
-index.html
-register_bundle.js
-main.go
func handlerequests (){
myRouter := mux.NewRouter().StrictSlash(true)
myRouter.Handle("/", http.FileServer(http.Dir("./static")))
myRouter.HandleFunc("/transaction", transactionHandler)
log.Fatal(http.ListenAndServe(":8080",myRouter))
}
my HTML files have links like these, (showing index.html)
<!-- CSS Files -->
<link href="./assets/css/bootstrap.min.css" rel="stylesheet" />
<link href="./assets/css/paper-dashboard.css?v=2.1.1" rel="stylesheet" />
<!--JS files -->
<script src="./assets/demo/demo.js"></script>
<!--Main Script file for the page -->
<script src="./register_bundle.js"></script>
errors shown here
The problem is browser cannot find those JS and CSS files.
fs := http.FileServer(http.Dir("./static"))
MUXRouter.Handle("/", fs)
MUXRouter.PathPrefix("/assets/").Handler(fs)
MUXRouter.PathPrefix("/pages/").Handler(fs)
MUXRouter.Handle("/register_bundle.js", fs)
That way a GET request to http://[host]:[port]/css/style.css will return style.css from the relative ./static/css/ directory. The above code is working in my sample program.
try
gor:= mux.NewRouter().StrictSlash(true)
fs := http.FileServer(http.Dir("./static"))
gor.PathPrefix("/transaction").Handler(fs)
it probably should work if it doesnt just try reading documentation for http.FileServer
did you try serving a handler fot your resources folder?
sdir := "/resources/"
myRouter.PathPrefix(sdir).Handler(http.StripPrefix(sdir, http.FileServer(http.Dir("."+sdir))))
this allow you to access the foloder as a subdomain.

Extending Airflow Web UI and adding Static HTML pages and CSS

I have been trying to extend the UI of airflow by adding airflow plugins. My goal is to be able to show internal project documentation from airflow. I am using MKDocs for this.
I have followed the airflow documentation process(https://airflow.apache.org/plugins.html#example) and created a plugin successfully. The link is appearing and when I click on it, it takes me to index.html. My problem is it is not rendering any CSS, images and other .md or .html files. They are all in the same folder.
I found a similar thread on StackOverflow (Airlfow serving static html directory). I have tried to follow the solution posted there, but it is not helping.
I have also posted the query on the airflow help forum but I have not received any solution yet. I would appreciate any leads. Thank you.
#Creating a flask appbuilder BaseView
class TestAppBuilderBaseView(AppBuilderBaseView):
template_folder = '/home/airflow/airflow/plugins/test-project/site'
#expose("/")
def list(self):
return self.render_template("index.html", content="")
v_appbuilder_view = TestAppBuilderBaseView()
v_appbuilder_package = {"name": "Test View",
"category": "Test Plugin",
"view": v_appbuilder_view}
Plugin Class:
# Defining the plugin class
class AirflowTestPlugin(AirflowPlugin):
name = "test_plugin"
appbuilder_views = [v_appbuilder_package]
Faced a similar issue: I had to show images in doc_md of a DAG.
Long story short we need to configure a Blueprint properly to host static files and templates.
Following works for me for airflow=1.10.14.
test_plugin.py
from airflow.plugins_manager import AirflowPlugin
from flask import Blueprint
from flask_admin import base, BaseView, expose
# Creating a flask admin BaseView
class TestView(BaseView):
#expose('/')
def list(self):
return self.render("index.html", content="")
v = TestView(category="Test Plugin", name="Test View")
# Creating a flask blueprint to integrate the templates and static folder
bp = Blueprint(
"test_blueprint", __name__,
template_folder='templates/testview',
static_folder='static/testview',
static_url_path='/admin/testview/static')
class AirflowTestPlugin(AirflowPlugin):
name = "test_plugin"
admin_views = [v]
flask_blueprints = [bp]
index.html
<!DOCTYPE html>
<HTML>
<body>
<p>This is a paragraph.</p>
<img alt="initial-state" src="static/img/initial-state.png">
</body>
</html>
Directory Structure
airflow
- plugins
- test_plugin.py
- templates
- testview
- index.html
- static
- testview
- img
- initial-state.png

CSS versioning on Google App Engine

I've read that I can use /stylesheets/default.css?{{ App.Version }} for the versioning of the css files on Google App Engine. How does it work? I tried putting the ?{{ App.Version }} at the end of my css files but it brokes the whole page. Am I doing something wrong?
You don't put version info at the end of your filename, you just append a "version" query parameter when you reference the CSS resources.
Don't change CSS/JS (or other static file) filenames, just change contents
Refer to those files in your HTML via <link rel="stylesheet" type="text/css" href="//host/path/file.css?version">
Everytime you change contents you increase version. You could also use application versions, but you'd have to make sure they do not repeat.
Default caching on static files is 10min. You can set your own cache expiration.
You should append the version number, i.e. (for Java):
String version = SystemProperty.applicationVersion.get();
You can append this string to your CSS file name in your host page.

How do I make css load from a database with codeigniter

I am developing a CMS so I need each respective site's global css file to be stored in a database and loaded. I have a controller called util and the method is called sitecss. So my main wrapper view looks like this:
<link rel="stylesheet" type="text/css" href="/util/sitecss">
The css loads, but has no effect. If I view source on the page, and then click on the link, I can see the css just fine. So I know it is being loaded. Is it something about it not coming from a file? Or perhaps the browser assuming it is cached when it is not?
If I make a static file and change to above to
<link rel="stylesheet" type="text/css" href="/css/site.css">
everything works just fine. If I do this in .NET with a handler ashx file, it works fine. It is in php with Codeigniter that I am having the problem. I know someone will ask "why don't you just make static files?" and the answer is, it is not practical for this application. This is for thousands of sites with very rapid deployment.
Thanks in advance!
EDIT:
My controller method looks like this:
function sitecss() {
$cssdata = $this->cmsutils->loadCss($this->session->userdata('sitecss'));
echo $cssdata;
}
So can I just echo a mime-type first? It doesn't seem like this will work as I am making this call within the
write a caching library to pull from the database and create a css file in a cache folder.
You will need:
Library Class
Interact with the css and create a form to perform CRUD
handle cache file monitoring CRUD (every hour or, even on every C,U,D of the form)
Inject the stylesheet cache file into the DOM view
Model
Interact with the database and perform CRUD operations
return data to the Controller for creating the cache file
View
parse out the values into a css stylesheet file format
The other option is to define a mime type with a controller and just load a view with the stylesheet properly formatted. No writing to the filesystem or anything.. Add a .css extension to the end of the URI and call it good...
I do this exact same thing for an app that I just released. I have a form in a view on the admin section of the app that has specified textfields. The user inputs hexadecimal color codes and then it saves/updates the data in the database. The library then creates a cached css file that is referenced in the header view. We did this to eliminate the need for us to add a .gitignore file in a special directory when we deploy the app to several clients.
Could you just load the css by passing it to the view from the controller and then echo it in the header somewhere or somewhere else in the view like this:
controller:
$data['css_rules'] = $this->yourcssmodel->get_css_rules_function();
$this->load->view('yadayadayada, $data);
view: (likely in header)
?>
<style>
<?echo $css_rules;?>
</style>

Resources