This is the template I've written
{% extends 'base.html' %}
{% load static %}
{% block body %}
<div class="row hide-on-mobile" style="height: 100vh">
<div class="col-auto p-5">
<img src="{% static 'logo.svg' %}" class="mb-5 hide-on-mobile" style="height: 84px"/>
{% block form %} {% endblock %}
</div>
<div class="col bg-brown p-5">
<div class="d-flex flex-column justify-content-between" style="height: 100%">
<h1 class="text-blue mb-3">It's always Ups<br>and Downs</h1>
<img src="{% static 'investing_illustration.svg' %}" class="auth-illustration"/>
</div>
</div>
</div>
<div class="hide-on-desktop" style="height: 100vh; position: relative">
<div class="d-flex flex-column bg-brown h-100 p-3 align-items-start">
<img src="{% static 'logo.svg' %}" class="mb-3 hide-on-desktop" style="height: 56px"/>
<p class="text-blue mb-3">It's always Ups<br>and Downs</p>
<img src="{% static 'investing_illustration.svg' %}" class="auth-illustration"/>
</div>
<div style="position: absolute; bottom: 0; z-index: 99; background: white; width: 100%">
<div class="d-flex flex-column p-4">
<p class="mb-4">Login</p>
{% block form %} {% endblock %}
</div>
</div>
</div>
{% endblock %}
I inherit this template in a page as follows:
{% extends 'auth_base.html' %}
{% block form %}
<p class="mb-4">Login</p>
<form class="pb-5">
<input type="text" class="form-control" name="email" placeholder="Enter your email">
<input type="password" class="form-control" name="password" placeholder="Enter your password">
<a class="text-blue">Forgot Password?</a>
<button type="submit" class="mt-3 btn btn-primary full-width">Login</button>
</form>
<div class="text-center">
<span>New Here? <a class="text-blue">Create an account</a></span>
</div>
{% endblock %}
The form block is used two times..because when I inherit the template I want to write only once..but display it in two different places in the same page. In this case, I've written different layouts for mobile and desktop show the user actually sees only once.
However, this code doesn't work and gives the following error:
django.template.exceptions.TemplateSyntaxError: 'block' tag with name 'form' appears more than once
How can I accomplish this?
It is possible to include partials multiple times. Partials work differently then blocks - not via inheritence, so you would have to change your template code a bit.
You would need to create a partial template for your form which would expect a certain context (the form probably), and you would include that template snippet twice in your main template:
<div class="d-flex flex-column p-4">
<p class="mb-4">Login</p>
{% include "_partials/login_form.html" %}
</div>
_partials/login_form.html would basically contain what you had now inside your form block.
https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#include
EDIT: as someone else pointed out - the question on multiple blocks with the same name has already been addressed before: https://stackoverflow.com/a/6427336/8401179
Related
In this Image, the data appears in vertical format. But I want data in horizontal format.
{% block title %}Blog{% endblock title %}
{% block body %}
<div class="container">
<div class="row">
{% for post in posts%}
<div class="col-lg-3">
<div class="card" style="width: 31.25rem;">
<!-- <div class="card"> -->
<img class="img-fluid card-img-top" src="https://images.unsplash.com/photo-1591154669695-5f2a8d20c089?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1887&q=80" alt="Card image cap" style="height: 250px;width: 500px;">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
</div>
</div>
</div>
</div>
<div class="post block">
<h2 class="subtitle">{{post.title}}</h2>
<small>Posted At {{post.date_added}}</small>
<p>{{post.intro}}</p>
Read More
</div>
{% endfor %}
{% endblock body %}
Hi There I'm Newbie In Django
How Can I Able To Appear Horizontally The Data Is Come From Database
So The Problem Was I Was Ending Row Inside For Loop To Create New Row You Have To Put For Loop Outside
Check This Code
{% extends 'base.html' %}
{% block title %}Blog{% endblock title %}
{% block body %}
<div class="container">
<div class="row">
{% for post in posts%}
<div class="col-lg-4">
<div class="card" style="width: 31.25rem;">
<!-- <div class="card"> -->
<img class="img-fluid card-img-top" src="https://images.unsplash.com/photo-1591154669695-5f2a8d20c089?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1887&q=80" alt="Card image cap" style="height: 250px;width: 500px;">
<div class="card-body">
<h5 class="card-title">{{post.title}}</h5>
{{post.date_added}}
<p class="card-text">{{post.intro}}</p>
Go somewhere
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock body %}
I have contents which were published using tinymce editor. Now, I want to show my content, that's why I have to use safe filter. But, whenever I'm trying to use slice or truncate filter, the template breaks.
this is my HTML code.
{% extends 'blog/frontend/layouts/base.html' %}
{% block content %}
<div class="card border-primary">
{% for post in posts %}
<div class="card my-2 mx-1">
<div class="card-body">
<h5 class="card-title">{{ post.title }}</h5>
<hr>
<img src="{{ post.image.url }}" alt="Card image" width="85%" class="mx-auto d-block">
<p class="card-text mt-1">
{{ post.content|truncatewords:50|safe}}
</p>
<hr>
<div class="d-flex">
<div class="px-2 py-0 my-auto">
<h6><i class="fas fa-user-edit"></i> {{ post.author.username }}</h6>
</div>
<div class="px-2 py-0 my-auto">
<h6>Date posted: <span class="text-info">{{ post.date_posted|date:"d M, Y" }}</span></h6>
</div>
<div class="ml-auto px-2 my-auto ">
Read more
</div>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
These are the image, first one without sliceor turncate filter, 2nd and 3rd one are with them.
Sorry, it was a stupid question to ask. Just in case anyone else is facing this kind of problem, use truncatechars_html or truncatewords_html just like this: {{ post.content|truncatewords_html:30|safe }} or {{ post.content|truncatechars_html:30|safe }}
I have a Django admin application that displays a jQuery dialog showing a page in an iframe inside the dialog that I show using:
function opendialog(page, dialog_title) {
var $dialog = $('#applied-assay')
.html('<iframe style="border: 0px; " src="' + page + '" width="100%" height="100%"></iframe>')
.dialog({
title: dialog_title,
autoOpen: false,
dialogClass: 'dialog_fixed,ui-widget-header',
modal: true,
draggable:true
});
$dialog.dialog('open');
}
function open_modal(url, title)
{
opendialog(url, title);
return false;
}
So far, so good. However the content of the iframe doesn't have any style:
The html template rendered inside the iframe looks like:
{% extends "admin/base_site.html" %}
{% load i18n admin_urls static admin_list %}
{% block extrahead %}
{{ block.super }}
{{ media.js }}
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/start/jquery-ui.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
{% endblock %}
<div class="modal-dialog">
<div class="modal-content">
<form role="form" action="{% url 'myapp:applied_assay' 1 %}" method="post">
<div class="modal-header">
<h3>Select applied assay type 1</h3>
</div>
<div class="modal-content">
{% csrf_token %}
<div class="panel panel-default">
<div class="panel-body">
{{ form.as_p }}
</div>
</div>
</div>
<div class="modal-footer">
<div class="col-lg-12 text-right">
<input type="submit" class="btn btn-primary" name="submit" value="Accept">
<button type="button" class="btn btn-default" onclick="return close_modal()">
Cancel
</button>
</div>
</div>
</form>
</div>
</div>
Any ideas?
You need to also include stylesheets in the header of your iframe and the respective classes in the html tags. You've only included a jquery-ui css file, but haven't used any of its classes in the iframe elements.
I am facing a problem building a django project:
the pull-right class in bootstrap 3 didn't work in django template pull-right
here is my code
{% block content %}
<div class="row">
<div class="col-sm-4 pull-right">
<h1>{{ title }}</h1>
<form method="POST" action="">
{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-primary" type="submit" value="Sign Up!">
</form>
</div>
</div>
{% endblock content %}
Put a div inside your column and apply the pull-right class on that one.
<div class="col-sm-4">
<div class="pull-right">
<!-- content here -->
</div>
</div>
Why are you using columns if you want to pull it right?
I'm using https://github.com/dyve/django-bootstrap3 plugin for my project with following code
<div class="container">
<div class="row">
<div class="jumbotron">
<div class="text-center">
<h1>{% block header_text %}{% endblock %}</h1>
<form method="post" class="form-inline">
{% csrf_token %}
Input: {% bootstrap_field form.input layout='inline' %}
Object: {% bootstrap_field form.object layout='inline' %}
<input type="submit" class="btn btn-xs btn-primary" value="Submit"/>
</form>
</div>
</div>
</div>
And get this fields as result How can i control width of them?
I tried to change it in .css file, but it only works with focus .form-control:focus {width: 320px}
Solved by addition "id": "input-form" to form widget and adjusting #input-form in .css file.