CSS in Django 2.0 - css

Here is a HTML code that my teammate wrote that uses CSS/Bootstrap.
How do I render it in Django?
It works when I open the file as HTML but I am not able to render it in Django.
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<!-- Bootstrap Core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css">
<link href="vendor/simple-line-icons/css/simple-line-icons.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/stylish-portfolio.min.css" rel="stylesheet">

in your settings put:
STATIC_URL = '/static/'
STATIC_ROOT = "path/to/your/static/folder"
in your root urls.py put after urlpatterns:
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
and create static folder in your root folder of project, and copy all your static files there.
After that in your template:
{% load static %}
<link href="{% static 'vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
Use like this.
If you have another questions, please read:
https://docs.djangoproject.com/en/2.1/howto/static-files/

Related

laravel 9 css not display properly

I have create a new laravel 9 project and I have encounter an css and js assets connection problem
At first, I have run
php artisan serve
to see if my project is working , and seems like everything is fine .
but when I just simply remove an part from the view , the css/js just get disabled to the view
even I undo the changes , the css are still not working . did any know whats happen?
here is my code for the css, and I believe it was properly connected
<head>
<meta charset="utf-8" />
<title>Admin Dashboard </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta content="Premium Multipurpose Admin & Dashboard Template" name="description" />
<meta content="Themesdesign" name="author" />
<!-- App favicon -->
<link rel="shortcut icon" href="{{ asset('backend/assets/images/favicon.ico')}}">
<!-- jquery.vectormap css -->
<link href="{{ asset('backend/assets/libs/admin-resources/jquery.vectormap/jquery-jvectormap-1.2.2.css')}}" rel="stylesheet" type="text/css" />
<!-- DataTables -->
<link href="{{ asset('backend/assets/libs/datatables.net-bs4/css/dataTables.bootstrap4.min.css')}}" rel="stylesheet" type="text/css" />
<!-- Responsive datatable examples -->
<link href="{{ asset('backend/assets/libs/datatables.net-responsive-bs4/css/responsive.bootstrap4.min.css')}}" rel="stylesheet" type="text/css" />
<!-- Bootstrap Css -->
<link href="{{ asset('backend/assets/css/bootstrap.min.css')}}" id="bootstrap-style" rel="stylesheet" type="text/css" />
<!-- Icons Css -->
<link href="{{ asset('backend/assets/css/icons.min.css')}}" rel="stylesheet" type="text/css" />
<!-- App Css-->
<link href="{{ asset('backend/assets/css/app.min.css')}}" id="app-style" rel="stylesheet" type="text/css" />
</head>
codes for vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: [
'resources/css/app.css',
'resources/js/app.js'
],
refresh: true,
}),
],
});
I have already do some research and seems like it might be related to the "vite", but It was something very strange to me . Could anyone tell me what should I do . Thank you in advance
From version 9.19.0 Laravel has drop the webpack and added the vitejs
To add css and js, you need to configure the vite
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel([
'backend/assets/libs/admin-resources/jquery.vectormap/jquery-jvectormap-1.2.2.css',
'backend/assets/libs/datatables.net-bs4/css/dataTables.bootstrap4.min.css',
'backend/assets/libs/datatables.net-responsive-bs4/css/responsive.bootstrap4.min.css',
'backend/assets/css/bootstrap.min.css',
'backend/assets/css/icons.min.css',
'backend/assets/css/app.min.css',
]),
],
});
To run the dev serve npm run dev
In blade
#vite([
'backend/assets/libs/admin-resources/jquery.vectormap/jquery-jvectormap-1.2.2.css',
'backend/assets/libs/datatables.net-bs4/css/dataTables.bootstrap4.min.css',
'backend/assets/libs/datatables.net-responsive-bs4/css/responsive.bootstrap4.min.css',
'backend/assets/css/bootstrap.min.css',
'backend/assets/css/icons.min.css',
'backend/assets/css/app.min.css',
]);
For more information laravel vite doc

css does not apply style to templates in django

The theme applied by css does not work. In the past it used to apply but i do not what i did that its not executable anymore. base.htmnl is core template for other pages.
In base.html i have :
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="{% static 'nana/css/main.css' %}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
<nav class='navbar mynav-fixed-top navbar-expand-lg bg-dark' role='navigation' id='navbar'>
<div class="container-fluid">
</div>
</nav>
<div class="container-fluid">
{% block content %}
<div class="main">
</div>
{% endblock %}
main.css
body{background-color: yellow}
settings.py :
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]
The css does not work. I do not knwow why the application does not apply changes in css file. Would appreciate your though and help how to fix it.
In general it is best to have the link to your own custom stylesheet (in your case that is main.css) after the other stylesheets in the head-section. The bootstrap stylesheets may overwrite your main.css thus making it useless.
You have the link to main.css before the links to bootstrap.
You better set STATIC_ROOT instead of STATICFILES_DIRS if this is the only folder with static files in your project:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
and change this {% load staticfiles %} to this {% load static %} in your templates - load staticfiles method is deprecated since 2.1.
Also, as already suggested in comments, put your stylesheet after all the thirdparty stylesheets - thus it will have higher priority. Also note that your style may have lower priority if any of prior stylesheets defines the same property with !important option.
You have not set the STATIC_ROOT, that's why your css is not showing up.
First Make sure in settings.py the debug setting is True
Debug=True
Put following code in settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticroot')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
in your main urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# other urls
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
and then call following command
>python manage.py collectstatic

Why does my CSS not work when I pass two parameters to app.get?

I'm creating an express app and my CSS code stops working whenever i add an ":ID" parameter to the URL. I know it's a filepath issue because bootstrap still comes in fine, but on the page with the ID parameter it shows this: "Refused to apply style from 'https://XXXXXX.c9users.io/unapproved/main.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled."
main.css is in my /public folder, but it looks like it's looking in a folder titled "unapproved".
I've tried changing the routing order, i've tried changing my app.use(express.static(__dirname)) code.
here's my app.get:
app.get("/unapproved/:id/", function(req, res){
var invoiceID = mongoose.mongo.ObjectId(req.params.id);
InvoiceObj.findById(invoiceID,function(err,foundInvoice){
if(err){
console.log(err);
}else{
res.render("invoiceScreen",{invoice:foundInvoice});
}
});
here's my html:
<% include partials/header %>
<h1><image src="<%= invoice.imageUrl %>"</h1>
<% include partials/footer %>
here's my header:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Invoice system</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="main.css">
<link href="https://fonts.googleapis.com/css?family=Cabin" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<script src='https://code.jquery.com/jquery-2.1.4.js'></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
I think change to css path absolute path will solve your problem
<link rel="stylesheet" href="/main.css">
In this kind of situation, it's better to use virtual path for your static files because relative path doens't work very well; like:
Assuming folder structure:
app.js
public
| +-- main.css
app.use('/static', express.static(__dirname + "/public"))
And in you html, make path absolute
<link rel="stylesheet" href="/static/main.css">
instead of using ./ or ../ before your assest(images / css / js ) for example.
./css/style.css or css/bootstrap.css ../images/example.png
you can use / for example
/css/style.css or /css/bootraps.css /images/example.png
/ will tell your app to look from root dir.

Polymer PWA hosted on firebase shows blank screen in Safari mobile iOS but works perfect in Chrome (both mac book, windows, android)

Polymer PWA app works perfectly fine in MacBook Chrome, Windows Chrome, Android Chrome browser but shows blank screen in MacBook Safari, iPhone Safari and iPhone Chrome.
Zero Errors or Zero warnings shown in web inspector.
HTML page - index.html
<!doctype html>
<!-- declare file type -->
<html lang="en">
<head>
<meta content-type="text/javascript" charset="utf-8">
<meta name="generator" content="App">
<meta name="viewport" content="width=device-width, minimum-scale=1,
initial-scale=1, user-scalable=yes">
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>MyApp</title>
<meta name="description" content="My App description">
<base href="/">
<link rel="shortcut icon" sizes="24x24" href="/images/6e156046-3456-
614b-4981-e136dfbf7d18.webPlatform.png">
<link rel="manifest" href="manifest.json">
<!-- Add to homescreen for Chrome on Android. Fallback for
manifest.json -->
<meta name="mobile-web-app-capable" content="yes">
<meta name="application-name" content="App">
<!-- Add to homescreen for Safari on iOS -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-
translucent">
<meta name="apple-mobile-web-app-title" content="App">
<!-- Homescreen icons -->
<link rel='icon' sizes='192x192' href='/images/27ab6cb7-8da3-a597-
b536-2edff24c3ac6.webPlatform.png'>
<link rel="apple-touch-icon" href="/images/c9d9ff33-7849-0621-0359-
e5e70df7cbf8.webPlatform.png">
<link rel="apple-touch-icon" sizes="72x72" href="/images/31b30d4f-
08f3-4faa-1fc2-15642e70b367.webPlatform.png">
<link rel="apple-touch-icon" sizes="96x96" href="/images/32f6784e-
c13e-81a0-1721-594d5908c4ed.webPlatform.png">
<link rel="apple-touch-icon" sizes="144x144" href="/images/ece2893b-
2863-9dfc-afe6-9c2a2961e540.webPlatform.png">
<link rel="apple-touch-icon" sizes="192x192" href="/images/27ab6cb7-
8da3-a597-b536-2edff24c3ac6.webPlatform.png">
<!-- Tile icon for Windows 8 (144x144 tile color) -->
<meta name="msapplication-TileImage" content="/images/ece2893b-2863-
9dfc-afe6-9c2a2961e540.webPlatform.png">
<meta name="msapplication-TileColor" content="#3f51b5">
<meta name="msapplication-tap-highlight" content="no">
<meta name="msapplication-square70x70logo"
content="/images/31b30d4f-08f3-4faa-1fc2-
15642e70b367.webPlatform.png">
<meta name="msapplication-square152x152logo"
content="/images/7ec23c59-18dd-0218-3cad-
09183db6b7a2.webPlatform.png">
<script>
window.Polymer = {rootPath: '/'};
// Load and register pre-caching Service Worker
if('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js', { scope: '/' })
.then(function(registration) {
console.log('Service Worker Registered - ' +
registration.scope);
});
navigator.serviceWorker.ready.then(function(registration) {
console.log('Service Worker Ready');
});
}
</script>
<script>
if (!window.HTMLImports) {
HTMLImports={};
}
</script>
<link rel="import" href="bower_components/polymer/polymer-
element.html">
<link rel="import" href="bower_components/paper-toast/paper-
toast.html">
<link rel="import" href="bower_components/platinum-sw/platinum-sw-
cache.html">
<link rel="import" href="bower_components/platinum-sw/platinum-sw-
register.html">
<link rel="import" href="ice.html">
<!-- Load your application shell -->
<link rel="import" href="my-app.html">
<!-- Add any global styles for body, document, etc. -->
<style>
body {
margin: 0;
font-family: 'Roboto', 'Noto', sans-serif;
line-height: 1.0;
min-height: 100vh;
background-color: #eeeeee;
}
</style>
</head>
<body>
<paper-toast id="toast" text="Hi, welcome to BizRec!"></paper-
toast>
<my-app></my-app>
Bower.json -->
"dependencies": {
"app-layout": "PolymerElements/app-layout#^2.0.0",
"app-route": "PolymerElements/app-route#^2.0.0",
"iron-flex-layout": "PolymerElements/iron-flex-layout#^2.0.0",
"iron-iconset-svg": "PolymerElements/iron-iconset-svg#^2.0.0",
"iron-media-query": "PolymerElements/iron-media-query#^2.0.0",
"iron-pages": "PolymerElements/iron-pages#^2.0.0",
"iron-selector": "PolymerElements/iron-selector#^2.0.0",
"paper-icon-button": "PolymerElements/paper-icon-button#^2.0.1",
"polymer": "Polymer/polymer#^2.0.0",
"webcomponentsjs": "webcomponents/webcomponentsjs#^1.0.0",
"polymerfire": "firebase/polymerfire#^2.2.0",
"paper-button": "PolymerElements/paper-button#^2.0.0",
"paper-input": "PolymerElements/paper-input#^2.0.2",
"iron-icons": "PolymerElements/iron-icons#^2.0.1",
"iron-ajax": "PolymerElements/iron-ajax#^2.0.5",
"paper-card": "PolymerElements/paper-card#^2.0.0",
"paper-checkbox": "PolymerElements/paper-checkbox#^2.0.1",
"iron-localstorage": "PolymerElements/iron-localstorage#^2.0.0",
"paper-menu-button": "PolymerElements/paper-menu-button#^2.0.0",
"paper-item": "PolymerElements/paper-item#^2.0.0",
"paper-toast": "PolymerElements/paper-toast#^2.0.0",
"paper-fab-speed-dial": "Collaborne/paper-fab-speed-dial#^2.3.1",
"paper-avatar": "NeilujD/paper-avatar#^2.0.1",
"app-toast": "jifalops/app-toast#^0.3.1",
"paper-listbox": "PolymerElements/paper-listbox#^2.0.0",
"paper-dialog": "PolymerElements/paper-dialog#^2.0.0",
"paper-tooltip": "PolymerElements/paper-tooltip#^2.0.1",
"neon-animation": "PolymerElements/neon-animation#^2.0.1",
"paper-toolbar": "PolymerElements/paper-toolbar#^2.0.0",
"iron-autogrow-textarea": "PolymerElements/iron-autogrow-textarea#^2.1.0",
"vaadin-date-picker": "vaadin/vaadin-date-picker#^2.0.4",
"d3-progress-meter": "Collaborne/d3-progress-meter#^1.1.1",
"paper-pulsating-progress": "Collaborne/paper-pulsating-progress#^2.0.2",
"paper-spinner": "PolymerElements/paper-spinner#^2.0.0",
"paper-toggle-button": "PolymerElements/paper-toggle-button#^2.0.0",
"iron-collapse": "PolymerElements/iron-collapse#^2.0.0",
"platinum-sw": "PolymerElements/platinum-sw#^2.0.0"}
There seems to be some issue in html imports or rel link that I am not able to check. Has anyone had similar issues? any fix recommended.
I am using Firebase deploy command to host the PWA on firebase.
Thanks,
V
Google Chrome is currently the only browser that has a native support for web components features (templates, HTML imports, custom elements and shadow DOM). For the other browsers you need to include a polyfill (webcomponentsjs) before importing any web component.
<script src="webcomponents-lite.js"></script>
<link rel="import" src="my-element.html">
You can see browsers support for Polymer 1.0 here.
And for 2.0 here.

View can't find layout page

My view can't find layout. But everything looks fine. My structure is shown below:
And my view is shown below:
#model xxx.Web.Model.Home.IndexModel
#{
ViewBag.Title = "Index";
Layout = "~/Views/_MainLayout.cshtml";
}
And my layout is shown below:
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="utf-8">
<title>Bootstrappage:Free Bootstrap Template (bootstrap 2.3.1 version)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!--
<link rel="stylesheet" href="themes/style/bootstrap.min.css" />
<link rel="stylesheet" href="themes/style/bootstrap-responsive.min.css" />
-->
<link rel="stylesheet/less" type="text/css" href="../../themes/less/bootstrap.less">
<script src="../../themes/js/less/less.js" type="text/javascript"></script>
<link rel="shortcut icon" href="assets/ico/favicon.ico">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png">
<script type="text/javascript">
$(function () {
#RenderSection("DocumentReady", false)
});
</script>
</head>
<body data-offset="40">
#{Html.RenderAction("Header", "Partial");}
#{Html.RenderAction("Navbar", "Partial");}
#RenderBody()
#{Html.RenderAction("Footer", "Partial");}
<script src="themes/js/lib/jquery-1.9.1.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
Also my controller is shown below:
public ActionResult Index()
{
IndexModel model = new IndexModel();
HomeModelFactory modelFactory = new HomeModelFactory();
model.Files = modelFactory.CreateIndexModel();
return View(model);
}
And exception:
Why I am getting this exception? It was working well.
And the full contents of the View / Controller as well please?
Some possible scenarios without looking at the View / Controller are;
- Some sections are missing (e.g. "DocumentReady")
- The view is trying to resolve model properties which are not set (i.e null)
You might check your properties setting on your _MainLayout.cshtml file. I had this same problem (the Layout page could not be found in the search path) until I set the VisualStudio properties BuildAction to Content. I had copied the file and renamed it, and the property had been set to 'none'.
This is your problem: Layout = "~/Views/_MainLayout.cshtml";
The _Layout reference should be Layout = "~/Views/Shared/_MainLayout.cshtml";
Dont make these type of mistakes again!

Resources