Connecting html with css using django - html

This is the html file that i'm trying to add a star rating for
book_detail.html
{% extends "base.html" %}
{% load static %}
{% block title %}
Block Detail Page
{% endblock title %}
{% block sidenav %}
{% for item in item_list %}
<li>
{{ item.item }}
</li>
{% endfor %}
{% endblock sidenav %}
{% block content %}
<h1 align="center"> Book Detail </h1>
<table align="center" border="2" width="400">
<tr>
<td>
{{ book.name }}
</td>
</tr>
<tr>
<td>
<img src="{% static book.pic_path %}" width="100">
</td>
</tr>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>
{{ book.username }}
<div id="full-stars-example">
<div class="rating-group">
<input class="rating__input rating__input--none" name="rating" id="rating-none" value="0"
type="radio">
<label aria-label="No rating" class="rating__label" for="rating-none"><i
class="rating__icon rating__icon--none fa fa-ban"></i></label>
<label aria-label="1 star" class="rating__label" for="rating-1"><i
class="rating__icon rating__icon--star fa fa-star"></i></label>
<input class="rating__input" name="rating" id="rating-1" value="1" type="radio">
<label aria-label="2 stars" class="rating__label" for="rating-2"><i
class="rating__icon rating__icon--star fa fa-star"></i></label>
<input class="rating__input" name="rating" id="rating-2" value="2" type="radio">
<label aria-label="3 stars" class="rating__label" for="rating-3"><i
class="rating__icon rating__icon--star fa fa-star"></i></label>
<input class="rating__input" name="rating" id="rating-3" value="3" type="radio" checked>
<label aria-label="4 stars" class="rating__label" for="rating-4"><i
class="rating__icon rating__icon--star fa fa-star"></i></label>
<input class="rating__input" name="rating" id="rating-4" value="4" type="radio">
<label aria-label="5 stars" class="rating__label" for="rating-5"><i
class="rating__icon rating__icon--star fa fa-star"></i></label>
<input class="rating__input" name="rating" id="rating-5" value="5" type="radio">
</div>
</div>
</td>
</tr>
</table>
{% endblock content %}
This is the css file that i trying to connect with the star rating on my html file on main.css
#charset "utf-8";
#header {
border-style: none;
width: 800px;
height: auto;
}
#wrapper {
margin-top: 8px;
margin-left: auto;
margin-right: auto;
background-color: #FFFFFF;
width: 800px;
}
#leftsidebar {
width: 180px;
height: 350px;
float: left;
}
#nav li {
padding-top: 10px;
padding-bottom: 10px;
list-style-type: none;
border-bottom: thin solid #5F5F5F;
color: #4C4C4C
left: 8px;
list-style-position: inside;
margin-left: -10px;
}
#main{
width: 560px;
float: left;
margin-left: 20px;
margin-right: 10px;
padding-right:10px;
}
#footer{
text-align: center;
margin-top: 5px;
padding-top: 10px;
padding-bottom: 10px;
border-top: thin solid #BBBBBB;
clear: both;
color: #969696;
}
#full-stars-example {
/* use display:inline-flex to prevent whitespace issues. alternatively, you can put all the children of .rating-group on a single line */
.rating-group {
display: inline-flex;
}
/* make hover effect work properly in IE */
.rating__icon {
pointer-events: none;
}
/* hide radio inputs */
.rating__input {
position: absolute !important;
left: -9999px !important;
}
/* set icon padding and size */
.rating__label {
cursor: pointer;
padding: 0 0.1em;
font-size: 2rem;
}
/* set default star color */
.rating__icon--star {
color: orange;
}
/* set color of none icon when unchecked */
.rating__icon--none {
color: #eee;
}
/* if none icon is checked, make it red */
.rating__input--none:checked + .rating__label .rating__icon--none {
color: red;
}
/* if any input is checked, make its following siblings grey */
.rating__input:checked ~ .rating__label .rating__icon--star {
color: #ddd;
}
/* make all stars orange on rating group hover */
.rating-group:hover .rating__label .rating__icon--star {
color: orange;
}
/* make hovered input's following siblings grey on hover */
.rating__input:hover ~ .rating__label .rating__icon--star {
color: #ddd;
}
/* make none icon grey on rating group hover */
.rating-group:hover .rating__input--none:not(:hover) + .rating__label .rating__icon--none {
color: #eee;
}
/* make none icon red on hover */
.rating__input--none:hover + .rating__label .rating__icon--none {
color: red;
}
}
I'm having trouble making the star rating in the html file connect with the CSS file I have.

In the head of your base.html add:
{% block head %}{% endblock head %}
Then in your book_detail.html add the link to your css:
{% block head %}
<link rel="stylesheet" type="text/css" href="{% static 'foldername/main.css' %}">
{% endblock %}

Related

Django templates not rendering CSS when template variables are used

I am trying to build a paywall using HTML and CSS in Django, but somehow the CSS that is supposed to fade portions of the text does not render. Here is what I have:
<style>
.adjust-image {
display: block;
width: auto;
max-width: 100%;
max-height: 1000px;
}
.blur-text {
filter: blur(5px);
user-select: none;
pointer-events: none;
}
.fade-out {
user-select: none;
position: relative;
}
.fade-out:after {
content: "";
height: 75%;
width: 100%;
position: absolute;
bottom: 0;
left: 0;
background: linear-gradient(to bottom, rgba(255,255,255,0), #fff);
}
#paywall-wrapper {
padding: 35px;
border-radius: 5px;
position: relative;
margin-bottom: 30px;
box-shadow: 0px 0px 20px -1px rgba(0,0,0,0.20);
}
#paywall-wrapper * {
text-align: center;
}
#paywall-wrapper .btn {
left: 50%;
transform: translateX(-50%);
position: relative;
}
</style>
<div class="display-margins">
<div>
<h5>{{solution.questionToProblem}}</h5>
<h6><b>Solution</b></h6>
{% if request.user.is_anonymous or solution.free != True and request.user.is_subscriber != True %}
<p class= "format-paragraph fade-out:after">{{solution.solutionToProblem|safe}}</p>
<div id="paywall-wrapper">
<h4>Premium Subscription</h4>
<p>To view this solution, get our subscription for just <strong>$17</strong>.</p>
<a class="btn btn-primary text-light" href = "{% url 'subscribe' %}">Subscribe</a>
</div>
{% else %}
<p class="noselect format-paragraph">{{solution.solutionToProblem|safe}}</p>
{% for s in solution_images %}
<img class = "adjust-image noselect" src="{{ s.photo.url }}">
<br></br>
{% endfor %}
{% endif %}
{% render_comments solution request %}
<br class= "extra_spacing"></br>
</div>
</div>
Ideally, the text for paywall is supposed to fade out like this:
But it doesn't render when I have template variables in my <p> tags:
Surprisingly enough, if I type out content in my <p> tags manually, it works. But when I use <p>{{random content}}</p>, it doesn't work.
What am I doing wrong with my CSS or my HTML when I say <p class= "format-paragraph fade-out:after">{{solution.solutionToProblem|safe}}</p>?
Make sure when you want CSS integrated in Django, and you have HTML in your template variable, use:
{{put_your_template_variable|striptags}}
instead of safe.

How to force two buttons into one line

Below you see the cell where the buttons (bootstrap 4) are. The other cells were not printed. The EDIT and DELETE button are one over the other (2 lines). How to force them to the same line?
<div class="container">
<table class="table table-striped">
..deleted...
<tbody>
..deleted...
<tr>
<div>
<form method="GET">
{% csrf_token %}
<button class="btn btn-outline-secondary btn-sm" type="submit" name="edit"
value={{objekt.id}}>Edit</button>
</form>
<div id="colwrap">
<form method="POST">
{% csrf_token %}
<button class="btn btn-outline-secondary btn-sm" type="submit" name="delete"
value={{objekt.id}}>Delete</button>
</form>
</div>
</div>
</tr>
</tbody>
</table>
</div class="container">
I finally found an answer (https://www.w3schools.com/howto/howto_css_button_group.asp) (with additional CSS).
In the html:
<div class="btn-group">
<button>Apple</button>
<button>Samsung</button>
<button>Sony</button>
</div>
In the CSS (in my case in APPfolder/static/app.css):
.btn-group button {
background-color: #4CAF50; /* Green background */
border: 1px solid green; /* Green border */
color: white; /* White text */
padding: 10px 24px; /* Some padding */
cursor: pointer; /* Pointer/hand icon */
float: left; /* Float the buttons side by side */
}
.btn-group button:not(:last-child) {
border-right: none; /* Prevent double borders */
}
/* Clear floats (clearfix hack) */
.btn-group:after {
content: "";
clear: both;
display: table;
}
/* Add a background color on hover */
.btn-group button:hover {
background-color: #3e8e41;
}

How to group an icon and an input element

I'm trying to achieve the following:
Create 3 input elements in a row
Each should have a logo to the left of it, centered perfectly.
Each should have a border-bottom that spans the logo as well.
Like the following image:
However with my current code the images can't be centered and the border doesn't span them. Here's my code:
input {
border: none;
width: 250px;
background-color: #393d49;
border-bottom: 1px solid #767D93;
padding: 10px;
}
form img {
width: 24px;
height: 24px;
}
<form>
<img src="assets/images/envelope.png" alt="Envelope icon indicating user's E-Mail.">
<input type="email" placeholder="E-Mail"><br>
<img src="assets/images/locked.png" alt="Lock icon indicating user's Password.">
<input type="password" placeholder="Password"><br>
<img src="assets/images/avatar.png" alt="Avatar icon indicating user's Name.">
<input type="text" placeholder="Username"><br>
</form>
As it was suggested, I would also use the font-awesome library. But if your not comfortable with that idea, here is how you can do without.
form, .form-row, input {
background-color: #051024;
}
.input-icon, label, input {
display: inline-block;
}
form {
padding: 0.8em 1.2em;
}
.form-row {
padding: 0.8em 0;
padding-bottom: 0.2em;
}
.form-row:not(:last-child) {
border-bottom: solid #18273a 1px; /* Only the last row has a border */
}
.input-icon {
width: 15px;
height: 15px;
margin: 0 10px;
}
label {
max-width:4em; /* Or the maximum width you want your lebel to be */
min-width:4em; /* Same */
color:white;
font-weight: 100;
}
input {
border:none;
padding: 0.8em 0.5em;
color: #6691c9;
font-size: 15px;
outline: none; /* No glowing borders on chrome */
}
<form>
<div class="form-row">
<!-- Put your image here, like so -->
<img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
<label for="form-email">Email</label>
<input id="form-email" type="email">
</div>
<div class="form-row">
<img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
<label for="form-password">Password</label>
<input id="form-password"type="password" placeholder="(8 characters min)">
</div>
<div class="form-row">
<img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
<label for="form-user">User</label>
<input id="form-user" type="text"><br>
</div>
</form>
If you're feeling adventurous
Try bootstrap, it has all you need to create cool web sites (it also includes the font-awesome library).

Can't see background image on overlay

I have an overlay containing a django form and the background image doesn't show up. I have a silhouette image in the background and that fills the page, and ideally moves with the page size. Then a table on top in the centre of it containing my form which I have tried to make transparent.
I was blindly fiddling with the css yesterday and it has disappeared!
{% extends 'prodman/basenew.html' %}
{% load staticfiles %}
{% block pagetitle %}Time and Expenses{% endblock %}
{% block content %}
<iframe name="hiddenFrame" class="hide"></iframe>
<div id="overlay">
<form id= "person-form" target='hiddenFrame' method = 'POST' action="" class="overlay-form" enctype="multipart/form-data">{% csrf_token %}
<table id="add-person-form" class="overlay-table">
<tr></br></br>{{ personform.first_name }}</tr>
<tr></br></br>{{ personform.surname }}</tr>
<tr></br></br>{{ personform.email }}</tr>
<tr></br></br>{{ personform.position }}</tr>
<tr></br></br>{{ personform.contract_type }}</tr>
<tr></br></br>{{ personform.mentor }}</tr>
<tr></br></br><font color="white">Assign as a coach? </font>{{ personform.make_person_coach }}</tr>
</table>
</br>
<input type = "submit" id = "save" value = "Add person">
</br>
</br>
<strong>Click here to [<a href='#' onclick='overlay()'>close</a>]</strong>
</form>
</div>
<a href='#' class='class1' onclick='overlay()'>Add person</a>
<p>Some more html here....</p>
{% endblock %}
In my basenew.html I have
{% load staticfiles %}
<head>
<link rel='stylesheet' href="{% static 'tande/newtimesheets.css' %}" type='text/css' media='all'/>
</head>
...
Here is my stylesheet
#overlay {
visibility: hidden;
position: absolute;
left: 0px;
top: 0px;
width:100%;
height:100%;
text-align:center;
z-index: 1000;
background-image:url('{% static 'tande/images/silhouette.png' %}');
background-repeat: no-repeat;
background-size: 90%;
background-position: center;
}
#overlay div {
width:800px;
margin: 100px auto;
background: #cccccc;
border: 1px solid #000;
padding:15px;
text-align:center;
}
form.overlay-form {
width:800px;
margin: 350px auto;
}
table.overlay-table tr td {
background: rgb(54, 25, 25);
background: rgba(54, 25, 25, 0);
border-style: none;
margin-right: 40%;
margin-bottom: 30%;
position: relative;
text-align: center;
width: 800px;
}
add-person {
text-align: right;
margin-right: 10px;
width: 10px;
}
body {
height:100%;
margin:0;
padding:0;
}
Just in case it's helpful here is how the overlay is triggered:
function overlay() {
el = document.getElementById("overlay");
el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
}
Thanks!

Login forms show up sometimes, sometimes they don't [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm building a personal site using Flask and SQLAlchemy. I've been having problems getting my /login route to display properly. It worked, then I changed my .page class margin in my CSS from auto and then back again, and then my login forms stopped displaying entirely.
What I can't figure out is what I did to make them disappear -- I cut my server off a couple of times and restarted it, and the page didn't load. When I came back quite a bit later, I restarted my server again and the login page loaded. I thought that it was "solved", but then when I came back a couple of hours later, the login page won't load again. All of my other extended HTML pages load correctly, just not my login page with my login forms.
I don't have a link anchor for the login page -- I just access it by typing out the route (/login).
Here is my relevant code:
views.py (auth blueprint)
from flask import render_template, redirect, request, url_for, flash
from flask.ext.login import login_user
from . import auth
from ..models import User
from .forms import LoginForm
#auth.route('/login', methods =['GET', 'POST'])
def login():
form = LoginForm()
"""if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data.first())
if (user is not None) and user.verify_password( form.password.data):
login_user(user, form.remember_me.data)
return redirect(request.args.get('next') or url_for('main.index'))
flash('Invalid username or password.')"""
return render_template('auth/login.html', title = "Log in", form = form)
layout.html
<!DOCTYPE html>
<title>
{% block title %}
perSimmons
{% endblock %}
</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<body>
<div>
<ul id="nav">
<li id="banner">{ perSimmons }</li>
<li id="links">Blog</li>
<li id="links">Resume</li>
<li id="links">Contact</li>
<li id="links">About</li>
</ul>
<br>
<div class=page>
{% block content %}
{% endblock %}
</div>
</div>
</body>
login.html
{% extends "layout.html" %}
{% block title %}perSimmons - {{title}} {% endblock %}
{% block content %}
<div class=page-header>
<h3>Admin Login</h3>
</div>
<form action="" method="post" name="login">
<p id="user-login">
Username:<br>
{{form.email}}<br>
Password:<br>
{{form.password}}<br>
</p>
<p><input type="submit" value="Log in">{{form.remember_me}}Stay signed-in </p>
</form>
{% endblock %}
style.css
#font-face {
font-family: 'SourceCodeProRegular';
src: url('fonts/SourceCodePro-Regular.otf.woff');
src: local('SourceCodePro Regular'), local('SourceCodePro'), url('fonts/SourceCodePro-Regular.otf.woff') format('truetype');
}
body { font-family: 'SourceCodeProRegular'; background: #CC3300; }
a, h2 { color: #CC3300; }
h1, h2 { font-family: 'SourceCodeProRegular'; margin: 0; }
h1 { border-bottom: 2px solid #CC3300; }
h2 { font-size: 2em; font-weight: bold; }
h3 { color: #CC3300;}
li { color: #CC3300;}
dl { color: #CC3300;}
p { color: #CC3300;}
#nav {
width: 100%;
float: left;
margin: 0 0 1em 0;
padding: 0;
list-style: none;
background-color: #CC3300;
border-bottom: 5px solid #999966;
border-top: 5px solid #999966;
border-left: 5px solid #999966;
border-right: 5px solid #999966;
}
#nav #banner { font-weight:bold; }
#nav li { float:left; }
#nav li a {
display: block;
padding: 8px 15px;
text-decoration: none;
color: #FFFF66;
border-right: 2px solid #999966;
}
#nav li a:hover {
color:#CC3300;
background-color: #FFFF66;
}
.page {
margin: 2em auto;
width: 55em;
border: 5px solid #999966;
padding: 0.8em;
background: #FFFF66;
}
.entries {
list-style: none;
margin: 0;
padding: 0;
}
.entries li { margin: 0.8em 1.2em; }
.entries li h2 { margin-left: -1em;}
.add-entry {
font-size: 0.9em;
border-bottom: 1px solid #CC3300;
}
.flash {
font-color: #999966;
font-family: 'SourceCodeProRegular';
background: #CC3300;
padding: 0.5em;
border: 1px solid #aacbe2;
}
.error {
background: #f0d6d6;
padding: 0.5em;
}
It looks like you're missing the form.csrf_token.
My forms disappeared sporatically without this present. Try the following:
login.html
{% extends "layout.html" %}
{% block title %}perSimmons - {{title}} {% endblock %}
{% block content %}
<div class=page-header>
<h3>Admin Login</h3>
</div>
<form action="" method="post" name="login">
{{form.csrf_token}}
<p id="user-login">
Username:<br>
{{form.email}}<br>
Password:<br>
{{form.password}}<br>
</p>
<p><input type="submit" value="Log in">{{form.remember_me}}Stay signed-in </p>
</form>
{% endblock %}
Note the addition of the {{form.csrf_token}} line.
You need to provide this for the form to render correctly if you don't use the wtf.quickform method.
The correct route for login.html in the auth blueprint is /auth/login -- not /login. Everything loaded correctly when I made this change.