Hii I'm kind of new so I might be making a rookie mistake but I have this HTML :
{% extends 'base.html' %}
{% load static %}
{% block meta %}
<title>HOME</title>
{% endblock meta %}
{% block style %}
<link rel="stylesheet" href="{% static 'style.css' %}">
{% endblock %}
{% block content %}
<div class="content">
<h1>Test</h1>
</div>
{% endblock content %}
and this CSS
div.content {
background-color: #DFBEBE;
text-align: center;
padding: 10px;
height: 100%;
}
But it's not working... I tried with .content before but it also won't apply to my HTML. I applied a h1 css (with font) and it works just fine. It also works just fine with my previous projects(?) What did I do?
I am not sure what your file structure is, but try something like this to link your stylesheet:
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
url_for() is what I have always used for my django projects, and it has worked every time. Note: you will need to have the stylesheet placed in the 'static' folder (unless you change 'static' in the function to something else).
<link rel="stylesheet" type="text/css" href="index.css">
This is the easiest way to link them. Also check if the CSS and HTML are in the same folder
You're probably not linking it correctly to the CSS in the HTML. Make sure your path is correct. If it is, .content should work fine.
Here is the working JSFiddle with your code.
Related
I have a problem that I am reproducing with a simple example.
No matter what I do I do always have a page with my content inside a body (js, footer, etc) like the image attached.
Can anyone help me to figure this out? Thank you.
If I remove the body tag from base.html, django include a body tag itself that encapsulates the remaining content.
base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
<body>
{% block content %}
{% endblock content %}
</body>
<footer>
<h3>This is the footer</h3>
</footer>
<script src="{% static 'js/jquery-3.5.1.min.js' %}"></script>
<script src="{% static 'bootstrap-4.4.1/js/bootstrap.min.js' %}" ></script>
</html>
home.html:
{% extends 'base.html' %}
{% block content %}
<div>
<h1>This is the home body</h1>
</div>
{% endblock content %}
The render result:
This is not a django issue.
The html tag can only have a head and body tag inside it. Your footer and script should go inside the body.
Your web browser have detected that the structure of your document is not valid and have corrected it.
If you check the HTML source code of your page (instead of using the dev tools), you will see that the template is rendered as you write it.
So move all your page content in the body tag.
I am developing the website on python(Django). I am getting some difficulties to change the background color of the top menu on every page.
What I am doing is, I have an index.html page and templates which are aboutus, contactus and service. When I am on the home page then I am getting the gray background of the menu which is correct. Now I am on aboutus and I have to change the menu color from gray to black but I am getting the only gray.
So I want to know how do I change the class or override the CSS? Should I need to add the class to the body and then override the menu BG? How do I added the class to the body on each page dynamically?
index.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title%}Home{% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" type="text/css">
<link rel="stylesheet" href="{% static 'css/style.css'%}" type="text/css">
</head>
<body>
<div class="Wrpper">
<header class="bg_gray"><!--top menu code--></header>
{% block content %}
<!-- template code -->
{% endblock %}
<footer><!--footer code--></footer>
</div>
<script src="{% static 'js/jquery.min.js' %}"></script>
<script src="{% static 'js/main.js' %}"></script>
</body>
</html>
style.css
.bg_gray{background-color: #ccc;}
.bg_black{background-color: #000;}
aboutus.html (All template are same)
{% extends 'demo1/index.html' %}
{% load static %}
<title>{% block title %}About us{% endblock %}</title>
{% block content %}
<!--some code here-->
{% endblock %}
you can override the header's class using javascript
first give your header an id :
then include the javascript file:
in app.js
$(document).ready(function() {
$('#top-header').removeClass('bg_gray');
$('#top-header').addClass('bg_blak');
});
<link rel="stylesheet" href="{% static 'js/app.js' %}">
<header class="bg_gray" id="top-header"><!--top menu code--></header>
Give important to bg_black class it will override the css of bg_gray class property...
like this,
.bg_black{
background-color: #000!important;
}
html:
<header class="bg_gray bg_black"><!--top menu code--></header>
I just want my CSS to apply, not sure where I'm messing up...
Alright so in my base.html.twig I have defined this
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('web/css/main.css') }}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
{% endblock %}
The whole path to the main CSS file.
K:\phpstormproject\symfony\my-project\web\css\main.css
In the CSS file, the class is found when starting to type but the change is not being applied.
Here is a simple change
.font-weight-light {
font-size: 20px;
}
I tried deleting the cache but that doesn't really do the job, does it have anything to do that it's a bootstrap class?
Need some help with django. Seems that somehow i can't change the css of my home page.
These are my static settings:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
#'/var/www/static/',
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media_cdn")
In my index_home.html page i want to put a background image e.g. marea.jpg which is in static/img/marea.jpg.
<!--DOCTYPE html -->
<html>
<head>
{% load static %}
<title>Romanii din Italia</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="static/css/custom.css" />
<link href="{% static 'css/custom.css' %}" rel="stylesheet">
<link rel='stylesheet' href="/static/css/custom.css" />
</head>
<body>
<div class='container'>
<ol class='breadcrumb'>
<li><a href='{% url "posts:home" %}'>Acasa</a></li>
<li><a href='{% url "posts:list" %}'>Stiri</a></li>
{% if not request.user.is_authenticated %}
<li class='pull-right'><a href='{% url "register" %}'>Register</a></li>
<li class='pull-right'><a href='{% url "login" %}'>Login</a></li>
{% else %}
<li class='pull-right'><a href='{% url "logout" %}'>Logout</a></li>
{% endif %}
</ol>
</div>
<div class="container" >
<h2>Bine ati venit pe site-ul romanilor din Italia!</h2>
<div class="row" id="primapoza" style="background-image: url('/static/img/roma.jpg');background-repeat: no-repeat;background-size: cover;height: calc(100vh - 71px);">
<div class="col-xs-12 offset-xs-0 col-sm-4 offset-sm-4 section-title" >
<br><br>
<p id="banner-text"> Romani in Italia! </p>
<!-- <p id="banner-text" style="text-align: center;font-size:40px;!important;font-color:white;!important;margin-left: 100px;"> Romani in Italia! </p> -->
</div>
</div>
</div>
</body>
</html>
And this is my custom.css which doesn't change my css code. Can someone pls advise? Thank you in advance!
h1 {
color: #777777;
}
.wmd-panel{
margin-right: 0px !important;
margin-left: 0px !important;
}
.comment-reply{
display:none;
}
#banner-text{
font-color:white;!important;
font-size:40px;
}
I can customize the CSS only with inline code e.g <p id="banner-text" style="margin-left: 400;font-color: black;font-size:30">Romani in Italia!</p
Use {% load static %} and {% load staticfiles %} under head tag in html
Based on my understand of how {% load static %} is used and what the Django documentation is saying, I don't think you're using {% load static %} correctly. I've always used it right above whatever my static link were to be.
For example, in your html doc head, it should be
{% load static %}
<link rel='stylesheet' href='{% static "css/base.css" %}'>
Otherwise, I don't think your stylesheet is linked to your html doc. Try that and also correct that mistake anywhere else you may have it, and it should be working.
You can't have {% load static %} at the top of your file and expect it to apply for the whole page. It needs to be right above whatever your static file is, as many times as you use it.
I used to see the changes I was making yesterday. And now I change the simply the color and the other things no change. This is really annoying and happened to me multiple times. I don't see any overriding file either. I checked the other similar problems and questions but couldn't solve it. So, if you can give me the logic behind the problem and the solution so that I don't encounter it again that would be very kind.
Here's the shortened CSS code:
.page-header {
background-color: #421557;
margin-top: 0;
padding: 20px 20px 20px 40px;
}
And here's the HTML code:
{% load staticfiles %}
<html>
<head>
<title>Blog</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link href="https://fonts.googleapis.com/css?family=Muli" rel="stylesheet">
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
</head>
<body>
<div class="page-header">
<h1>Blog</h1>
</div>
{% for post in posts %}
<div class="post">
<p>published: {{ post.published_date }}</p>
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaksbr }}</p>
</div>
{% endfor %}
</body>
</html>
Whenever I mess up with Bootstrap, I get angry. I think I still didn't get the idea.