Django templates not rendering CSS when template variables are used - html

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.

Related

django forms - CSS alignment

I am pretty new to CSS, however I have a django modelformset_factory which i need to render and align all rendered fields in same line for each field, it has TextArea and Textinput, the problem as per the attached picture i tried to aligned all but they never get aligned together.
here my view:
from django.forms import modelformset_factory
def form_employee(request, employee_id):
employee = Employee.objects.get(pk=employee_id)
Employee_PM = PMF.objects.filter(badge=employee_id)
for pm in Employee_PM:
form_reference = pm.form_ref
emp_form = PMF_Form.objects.filter(form_ref=form_reference)
emp_summary = PMF_Summary.objects.filter(badge=employee_id)
PMF_formset = modelformset_factory(PMF_Form, fields=('objective','obj_desc','rating',), extra=0)
formset = PMF_formset(queryset=PMF_Form.objects.filter(form_ref=form_reference))
if request.method == "POST":
formset = PMF_formset(request.POST, queryset=PMF_Form.objects.filter(form_ref=form_reference))
if formset.is_valid():
formset.save()
return redirect('/')
for form in formset:
form.fields['objective'].widget.attrs['disabled'] = True
form.fields['obj_desc'].widget.attrs['disabled'] = True
context = {"Employee_PM":Employee_PM,"employee":employee,"emp_form":emp_form,"emp_summary":emp_summary,"formset":formset}
return render(request, 'PMS/form_employee.html',context)
\* {
padding: 2px;
margin: 0px;
margin-bottom: 4px;
background-color: black;
color: aliceblue;
box-sizing:border-box;
flex-direction: column;
}
.container {
background-color: black;
}
body {
background-color: black;
}
.form2 {
margin: 20px;
box-sizing: border-box;
width:100%;
resize: none;
overflow-wrap: break-word;
/\* display: flex; \*/
margin: auto;
}
input {
/\* width: 50%; \*/
display: inline-block;
width:15rem;
height: 70px;
position: relative;
}
textarea {
width: 50rem;
height: 70px;
resize: none;
}
}
span {
justify-content: center ;
display: flex;
}
<div class="container">
<h2>formset</h2>
<form method="POST" class="form2" enctype="multipart/form-data">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{% for field in form %}
{{field}}
{% endfor %}
{% endfor %}
<button type="submit">Submit</button>
</form>
a photo from the output
the main problem is mainly I need to give them all the same height and align them to same line
i think you should use css property called 'vertical-align'

div dropdown not showing

I am attempting to create a dropdown for a user. The problem is that it doesn't show up at all.
<div class="account-wrap">
<div class="inside-account trade-sell-div">
<a class = "trade-sell-button" href="{% url 'product' %}">Trade/Sell</a>
</div>
{% if user.is_authenticated %}
<div class = "inside-account chat-box-dropdown">
<a class="imgs-signed-in"><img style="height:45px !important;" src = "{% static "imgs/chat-box.png" %}" alt="Chat Box"></a>
</div>
<div class="inside-account user-dropdown">
<a class="imgs-signed-in user-icon-dropdown"><img src="{% static "imgs/user-icon.png" %}" alt="User"></a>
<div class="dropdown-frame">
<div id="triangle"></div>
<div class="dropdown-wrapper">
<hr>
</div>
</div>
</div>
{% else %}
<div class="inside-account">
Login
</div>
{% endif %}
</div>
css
.dropdown-frame{
position: absolute;
height: 40rem;
width: 10rem;
outline: 1px solid black;
}
#triangle {
width: 0;
height: 0;
content: '';
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-bottom: 20px solid white;
}
.dropdown-wrapper {
right: 0;
height: 40rem;
width: 10rem;
top: 56px;
border: 1px solid red;
z-index: 122;
}
Basically the triangle shows up on the nav bar but the part under the triangle does not show up at all. When I move the div elements outside the nav, it actually shows up. Does anyone have any explanation as to why my div dropdown-wrapper does not show up?
I believe you missed the . before dropdown-frame class
It should be {% if request.user.is_authenticated %} instead of {% if user.is_authenticated %}

Add Hover Feature On Product Image

I am trying to add a feature to show an alternative product picture when the cursor hovers over the product image in a collection view.
Whenever I view the page with the code below, the product images have shifted to the bottom of the container and also cover the Title/Price that was below them.
What can I correct to keep the pictures in their original alignment and have only the hover effect work?
Thank you!
Here is what I currently have in product-card-grid.liquid --
<div id="{{ wrapper_id }}" class="grid-view-item__image-wrapper js">
<div style="padding-top:{% unless product.featured_image == blank %}{{ 1 | divided_by: product.featured_image.aspect_ratio | times: 100}}%{% else %}100%{% endunless %};">
<div class="reveal">
<img id="{{ img_id }}"
class="grid-view-item__image lazyload"
src="{{ product.featured_image | img_url: '300x300' }}"
data-src="{{ img_url }}"
data-widths="[180, 360, 540, 720, 900, 1080, 1296, 1512, 1728, 2048]"
data-aspectratio="{{ product.featured_image.aspect_ratio }}"
data-sizes="auto"
alt="">
<img id="{{img_id}}"
class="hidden"
src="{{ product.images.last }}"
data-src="{{ img_url }} "
data-widths="[180, 360, 540, 720, 900, 1080, 1296, 1512, 1728, 2048]"
data-aspectratio="{{ product.featured_image.aspect_ratio }}"
data-sizes="auto"
alt="{{ product.images.last.alt | escape }}">
</div>
</div>
</div>
The css for "Reveal" is below:
/* ===============================================
// Reveal module
// =============================================== */
.reveal .hidden { display: block !important; visibility: visible !important;}
.product:hover .reveal img { opacity: 1; }
.reveal { position: relative; }
.reveal .hidden {
position: absolute;
z-index: -1;
top: 0;
width: 100%;
height: 100%;
opacity: 0;
-webkit-transition: opacity 0.3s ease-in-out;
-moz-transition: opacity 0.3s ease-in-out;
-o-transition: opacity 0.3s ease-in-out;
transition: opacity 0.3s ease-in-out;
}
.reveal:hover .hidden {
z-index: 100000;
opacity: 1;
}
.reveal .caption {
position: absolute;
top: 0;
display: table;
width: 100%;
height: 100%;
background-color: white; /* fallback for IE8 */
background-color: rgba(255, 255, 255, 0.7);
font: 13px/1.6 sans-serif;
text-transform: uppercase;
color: #333;
letter-spacing: 1px;
text-align: center;
text-rendering: optimizeLegibility;
}
.reveal .hidden .caption .centered {
display: table-cell;
vertical-align: middle;
}
#media (min-width: 480px) and (max-width: 979px) {
.reveal .caption {
font-size: 11px;
}
}
There's a lot going on in your CSS, but if I understand the sitaution correctly you want to achieve an effect like the one in this Codepen, right?
In your CSS, absolutely position two images inside of a div (with top: 0px and left: 0px) and change the opacity of the top image in the stack by hovering on the parent.
HTML:
<div class='reveal'>
<img src='image_src' class='bottom'>
<img src='image_src' class='top'>
</div>
CSS:
.top, .bottom {
position: absolute;
opacity: 1;
top: 0px;
left: 0px;
}
.frame:hover .top {
opacity: 0;
}
EDIT: Implementation w/ grid, as requested.
HTML:
<div class='grid'>
<div class='reveal'>
<img src='image_src' class='bottom'>
<img src='image_src' class='top'>
</div>
<div class='reveal'>
<img src='image_src' class='bottom'>
<img src='image_src' class='top'>
</div>
</div>
CSS:
.grid {
display: flex;
flex-wrap: wrap; /*makes the grid "responsive"*/
}
.reveal {
display: flex;
flex: 1 1 200px; /*last value must be picture width*/
height:200px; /* picture height*/
margin: 20px; /*aesthetic only*/
position: relative;
}
.top, .bottom {
position: absolute;
opacity: 1;
top: 0px;
left: 50%; /*centers pictures inside flex children (.reveal divs)*/
transform: translate(-50%, 0); /*see above*/
}
.reveal:hover .top {
opacity: 0;
}
Demo on Codepen
The above example uses flexbox to create a simple grid which will work with the (slightly modified) .reveal divs from my original post. If you're unfamiliar with flexbox syntax, there's an awesome free video course from Wes Bos that I highly recommend.
P.S. Note that the grid will only work "out of the box" if the images have a set pixel width, although you could easily support different images sizes with a little media query magic.
Got it to work with the following
In the CSS, I had the #media (hover:hover){} so that users do not have to double click an image on mobile to go to the product page.
CSS:
/* ===============================================
// Reveal module
// =============================================== */
#media (hover:hover) {
.has-secondary.grid-view-item__link img.secondary{
display:none;
}
}
#media (hover:hover) {
.has-secondary.grid-view-item__link:hover img.secondary{
display:block;
}
}
#media (hover:hover) {
.has-secondary.grid-view-item__link:hover img.grid-view-item__image{
display:none;
}
}
#media screen and (min-width:767px){
.has-secondary.grid-view-item__link img.secondary{
display:none;
}
.has-secondary.grid-view-item__link:hover img.secondary{
display:block;
}
.has-secondary.grid-view-item__link:hover img.grid-view-item__image{
display:none;
}
}
#media screen and (max-width:767px){
.has-secondary.grid-view-item__link img.secondary{
display:none;
}
}
Changed product-card-grid.liquid HTML/Liquid to the following:
{% unless grid_image_width %}
{%- assign grid_image_width = '600x600' -%}
{% endunless %}
<div class="grid-view-item{% unless product.available %} product-price--sold-out grid-view-item--sold-out{% endunless %}">
<a class="grid-view-item__link {% if product.images.size > 1 %} has-secondary{% endif %}" href="{{ product.url | within: collection }}">
<img class="grid-view-item__image" src="{{ product.featured_image.src | img_url: grid_image_width }}" alt="{{ product.featured_image.alt }}">
{% if product.images.size > 1 %}
<img class="secondary" src="{{ product.images.last | img_url: grid_image_width }}" alt="{{ product.images.last.alt | escape }}">
{% endif %}
</a>
</div>
<div class="h4 grid-view-item__title">
{{ product.title }}
</div>
{% if section.settings.show_vendor %}
<div class="grid-view-item__vendor">{{ product.vendor }}</div>
{% endif %}
<div class="grid-view-item__meta">
{% include 'product-price', variant: product %}
</div>

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.