Why bootstrap carousel and django not workig properly? - django

I am trying to provide bootstrap carousel backend with django.I have written a simple program view which will pass all images to template.But when i run the program i did't get output as expected, all images are rendered adjecent to one other. There was nothing like forward and backward button and all.And i am posting my view and template.
Thanks in advance.
Hope to here from you soon.
View:-
def TaggingView(request):
queryset = MyImage.objects.filter(is_tagged=False)
return render(request, "taggging.html", {'files': queryset})
Template :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div id="carouselInd" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
{% for file in files %}
{% with forloop.counter0 as i %}
<div class="carousel-item {% if i is 0 %}active{% endif %}">
<img class="d-block w-100" src="{{ file.image.url }}" alt="" style="height: 30rem;">
</div>
{% endwith %}
{% endfor %}
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</body>
</html>
output:-

Django work fine with Bootstrap carousel, you need to be aware with the active class like this :
<div id="carouselInd" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
{% for file in files %}
{% with forloop.counter0 as i %}
<div class="carousel-item {% if i is 0 %}active{% endif %}">
<img class="d-block w-100" src="{{ file.image.url }}" alt="
{{ file.name }}" style="height: 30rem;">
</div>
{% endwith %}
{% endfor %}
</div>
</div>
NB : The alt attribute is dynamic, you can use a static value if you want. And give a height to the image can help to contain the image too.

Related

Make a carousel that automatically uses all images in a folder

I have a django project that can put images I upload into a static file in my application folder. I was wondering if someone could point me in the right direction in making a carousel that automatically takes all images from that folder when the template is refreshed?
UPDATE: I've gotten the code to display the file names in a bootstrap carousel, but I can't get the images. can anyone help? The code for my template is as follows:
{% load staticfiles %}
{% load filename %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Minimal Django File Upload Example</title>
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="http://getbootstrap.com/examples/carousel/carousel.css" rel="stylesheet">
</head>
<body>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
{% for document in documents %}
<div class="item {% if forloop.first %} active {% endif %}">
<div class="row">
<div class="col">
{{document|filename}}
</div>
</div>
</div>
{% endfor %}
</div>
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<!-- /.carousel -->
<!-- List of uploaded documents -->
<!-- Upload form. Note enctype attribute! -->
<form action="{% url 'list' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload" /></p>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
<script src="http://getbootstrap.com/assets/js/docs.min.js"></script>
</body>
</html>

Django dynamic carousel uploads

I have a django project that can put images I upload into a static file in my application folder. I was wondering if someone could point me in the right direction in making a carousel that automatically takes all images from that folder when the template is refreshed?
I've gotten the code to display the file names in a bootstrap carousel, but I can't get the images. can anyone help? The code for my template is as follows:
{% load staticfiles %}
{% load filename %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Minimal Django File Upload Example</title>
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="http://getbootstrap.com/examples/carousel/carousel.css" rel="stylesheet">
</head>
<body>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
{% for document in documents %}
<div class="item {% if forloop.first %} active {% endif %}">
<div class="row">
<div class="col">
{{document|filename}}
</div>
</div>
</div>
{% endfor %}
</div>
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<!-- /.carousel -->
<!-- List of uploaded documents -->
<!-- Upload form. Note enctype attribute! -->
<form action="{% url 'list' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload" /></p>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
<script src="http://getbootstrap.com/assets/js/docs.min.js"></script>
</body>
</html>

Irrelevant Alert message before performing any image upload in django

i have been working on my image upload portion of my web app and developing it using Django.My Image Upload functionality working absolutely fine.No problem with that.
But in my image upload page i am getting unnecessary alert message at a time such as "You have been singed in","You have been signed out" after making a sign in and sign out and this alert message is appearing after entering in my image uplaod page,which is weired!.
Yeah i have set django message tag in my Image upload template to show alert message if user make any image upload action.But i don't want to get any alert message such as "You have been signed in" or "You have been signed out" or something irrelevant in my image upload page after entering in my image upload page to upload an image.But unfortunately its happening and after making lot's of googling and research ,i can't figure it out.
In mention i am using django form and django message framework.
This is my view for the image upload
def UserImageUpload(request):
if request.method == 'POST':
form = DocumentForm(request.POST,request.FILES)
if form.is_valid():
newdoc = Photo(photo = request.FILES['photo'],watermarked_image=request.FILES['photo'],user = request.user,name = request.POST['name'],description = request.POST['description'],keyword = request.POST['Image_Keyword'],uploaded_time=datetime.datetime.now(),Certified=request.POST['Certification'])
newdoc.save()
messages.success(request,'Your Image upload is waiting for Admin approval')
form = DocumentForm
else:
messages.error(request,'Please Complete All Fields or Only upload jpg file,because we are currently accepting only the jpg file!')
else:
form = DocumentForm()
uploaded_image = Photo.objects.all()
return render_to_response('myprofile/user_image_upload.html',{'uploaded_image':uploaded_image,'form':form},context_instance = RequestContext(request))
and this is Image upload form template
{% extends 'base.html'%}
{% block title%}User Image Upload {% endblock %}
{%block content%}
<div id="messages" style="margin-top:50px">
{% if messages %}
<!--<div class="row"> -->
{% for message in messages %}
<div class="alert alert-{{ message.tags }}">
<!--<p{%if message.tags=="success"%} class="alert alert-success" {%endif%}>{{message}}</p> -->
<button type="button" class="close" data-dismiss="alert"></button>
{{ message }}
</div>
{% endfor %}
<!--</div> -->
{% endif %}
</div>
<div class="container" style="margin-top:5%" ng-app="ImageUpload">
<div class="col-md-4 col-md-offset-4" ng-controller="ImagePreviewCtrl">
<div class="well" ng-show="show">
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.photo.label_tag }} {{ form.photo.help_text }}</p>
<ul class = 'unstyled'>
<li class = 'tust'>{{form.name.label}}{{ form.photo.errors }}</li>{{form.name}}<br/>
<li class = 'tust'>{{form.description.label}}{{ form.photo.errors }}</li>{{form.description}}<br/>
<li class = 'tust'>{{form.Image_Keyword.label}}{{ form.photo.errors }}</li>{{form.Image_Keyword}}<br/>
<li class = 'tust'>{{form.Certification.label}}{{ form.photo.errors }}</li>{{form.Certification}}<br/>
{{ form.photo }}
</ul>
<input type="submit" value="Upload" class="btn btn-success" />
</form>
</div>
</div>
</div>
{%endblock%}
UPDATE:
my base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="assets/ico/favicon.png">
<title>{%block title%}Medical Art{%endblock%}</title>
<!-- TEST OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO -->
{%block css%}
<!-- Bootstrap core CSS -->
<link type = 'text/css' href="{{STATIC_URL}}photo/css/bootstrap.css" rel="stylesheet">
<!-- Custom styles for this template -->
<!--<link type = 'text/css' href="{{STATIC_URL}}photo/css/main.css" rel="stylesheet">-->
<link type = 'text/css' href="{{STATIC_URL}}photo/css/font-awesome.min.css" rel="stylesheet">
<link type = 'text/css' href="{{STATIC_URL}}photo/css/main-profile.css" rel="stylesheet">
<link type = 'text/css' href="{{STATIC_URL}}photo/css/car.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Raleway:400,300,700' rel='stylesheet' type='text/css'>
{%endblock%}
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<script src="{{STATIC_URL}}photo/js/angular.min.js"></script>
</head>
<body>
<div>
{%block navi%}
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{% url 'home'%}"><strong>Medical Art</strong></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
{% if user.is_authenticated %}
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Photo</li>
<li>Upload</li>
</ul>
<div class="col-sm-3 col-md-3">
<!--<form class="navbar-form navbar-left" action='/search/search_result/' method='post' role="search">-->
<form class="navbar-form" action='/search/search_result/' method='post' role="search">
<div class="input-group">
{% csrf_token %}
<input type="text" class="form-control" placeholder="Search" name="search" id='search_item'>
<div class="input-group-btn">
<button type="submit" class="btn btn-default" ><span class="glyphicon glyphicon-search"></span></button>
</div>
</div>
</form>
</div>
<ul class="nav navbar-nav navbar-right">
<li><span style = "color:red; padding-left:5px;"> {{user.username}}!</span></li>
<li class="dropdown">
Picture Management<b class="caret"></b>
<ul class="dropdown-menu">
<li>
Buyer
<li class="divider"></li>
<li>> My Purchased Picture</li>
<li class="divider"></li>
</li>
<li>Contributor
<li class="divider"></li>
<li>> My Pending Pictures</li>
<li class="divider"></li>
<li>> My Approved Pictures</li>
<li class="divider"></li>
<li>> My earnings</li>
</li>
<li class="divider"></li>
<li>Log-Out</li>
</ul>
</li>
<li class="dropdown">
Account Mangement<b class="caret"></b>
<ul class="dropdown-menu">
<li>My Profile</li>
<li>Edit profile</li>
<li>Change password</li>
<li>Change email</li>
<li class="divider"></li>
<li>Log-Out</li>
</ul>
</li>
</ul>
</div>
{%else%}
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
</ul>
<div class="col-sm-3 col-md-3">
</div>
<ul class="nav navbar-nav navbar-right">
<li>Sign In</li>
<li>Sign Up</span></li>
</ul>
</div>
{% endif %}<!-- /.navbar-collapse -->
</nav>
{%endblock%}
</div>
<div>{% block content%}
<div class="endless_page_template">
{% include 'photo/endless.html' %}
</div>
{% endblock%}
</div>
<br>
{%block footer%}
<div class="footer navbar-fixed-bottom" style="background-color:black; text-align:center">
<h4 style = "color:#ffffff">Powered by- Medical Art - Copyright 2014</h4>
</div>
{%block js%}
<script src="{{STATIC_URL}}photo/js/modernizr.custom.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="{{STATIC_URL}}photo/js/bootstrap.min.js"></script>
<script src="{{STATIC_URL}}photo/js/main.js"></script>
<script src="{{STATIC_URL}}photo/js/masonry.pkgd.min.js"></script>
<script src="{{STATIC_URL}}photo/js/imagesloaded.js"></script>
<script src="{{STATIC_URL}}photo/js/imageupload.js"></script>
<script src="{{STATIC_URL}}photo/js/classie.js"></script>
<script src="{{STATIC_URL}}photo/js/AnimOnScroll.js"></script>
<script src="{{STATIC_URL}}photo/js/jquery.cookie.js"></script>
<script src="{{STATIC_URL}}photo/js/endless.js" type="text/javascript" charset="utf-8"> </script>
<script>$.endlessPaginate({paginateOnScroll: true,paginateOnScrollMargin: 20});</script>
<script>
new AnimOnScroll( document.getElementById( 'grid' ), {
minDuration : 0.4,
maxDuration : 0.7,
viewportFactor : 0.2
} );
</script>
<script>
$(document).ready(function() {
//Events that reset and restart the timer animation when the slides change
$("#transition-timer-carousel").on("slide.bs.carousel", function(event) {
//The animate class gets removed so that it jumps straight back to 0%
$(".transition-timer-carousel-progress-bar", this)
.removeClass("animate").css("width", "0%");
}).on("slid.bs.carousel", function(event) {
//The slide transition finished, so re-add the animate class so that
//the timer bar takes time to fill up
$(".transition-timer-carousel-progress-bar", this)
.addClass("animate").css("width", "100%");
});
//Kick off the initial slide animation when the document is ready
$(".transition-timer-carousel-progress-bar", "#transition-timer-carousel")
.css("width", "100%");
});
</script>
{%endblock%}
{%endblock%}
</body>
</html>
My guess is that your messages are piling up until you enter your upload page. Then they are displayed all at once.
Moving messages code to your base.html should solve this.
Edit
If you will place messages just before your content block it will solve the issue.
{% if messages %}
<div id="messages" style="margin-top:50px">
{% for message in messages %}
<div class="alert alert-{{ message.tags }}">
<button type="button" class="close" data-dismiss="alert"></button>
{{ message }}
</div>
{% endfor %}
</div>
{% endif %}
<div id="content">
{% block content%}
<div class="endless_page_template">
{% include 'photo/endless.html' %}
</div>
{% endblock%}
</div>

Django 1.5: When extending base.html, my <head> tag displays in my child's <body>

Edit: Adding the full versions of my base and child template.
I'm using Django 1.5.8 and have a base template (in template root) and sub folders for child templates which extend the base.
When I extend base.html all the contents of the base template show up in the body of the child template. This happens on all child pages except the index. Is there some Django template inheritance rule that I'm not aware of?
Here is my full base:
<!-- base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
{% load ganalytics %}
{% load twitter_tag %}
{% load compress %}
{% load tags %}
<meta charset="utf-8">
<!--<meta http-equiv="X-UA-Compatible" content="IE=edge">-->
<meta name="viewport" content="width=device-width, initial-scale=1">
{% block othermeta %}
<meta name="description" content="Welcome to Multimechanics">
<title>MultiMechanics</title>
<link rel="icon" type="image/png" href="{{ STATIC_URL }}ico/favicon.ico" />
<!--Needed for salesforce-->
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
{% endblock %}
<!-- Bootstrap core CSS -->
<!--<link href="css/bootstrap.min.css" rel="stylesheet">-->
{% compress css %}
<link href="{{ STATIC_URL }}css/style.css" rel="stylesheet">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<link href="{{ STATIC_URL }}css/animate.css" rel="stylesheet">
<link href="{{ STATIC_URL }}css/lightbox.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700' rel='stylesheet' type='text/css'>
{% endcompress %}
{% ganalytics %}
{% block otherheader %}{% endblock %}
</head>
<body>
<!-- Navigation -->
<header>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/"><img src="{{ STATIC_URL }}img/logo.png" alt="..."></a>
</div>
<div class="collapse navbar-collapse">
{% if user.is_authenticated %}
Log Out
{% else %}
Log In
{% endif %}
<ul class="nav navbar-nav navbar-right">
<li class='dropdown {% active request "^/faqs/$" %} {% active request "^/multiscale/$" %} {% active request "^/about-us/$" %}'>
About<b class="caret"></b>
<ul class="dropdown-menu">
{% if request.get_full_path == "/" %}
<li>Product Overview</li>
<li>Product Applications</li>
{% else %}
<li>Multimech Home</li>
{% endif %}
<li>MultiMech Details</li>
<li>What's Multiscale?</li>
<!--<li>Demos</li>
<li>Case Studies</li>-->
<li>Frequent Questions</li>
</ul>
</li>
<li class='dropdown {% active request "^/trueinnovation/$" %} {% active request "^/portfolio/$" %}'>
Showcases <b class="caret"></b>
<ul class="dropdown-menu">
{% if request.get_full_path == "/" %}
<li>Featured Demos</li>
{% endif %}
<li>Demo Gallery</li>
<li>Blog</li>
</ul>
</li>
<li class='dropdown {% active request "^/careers/$" %} {% active request "^/contact-us/$" %} {% active request "^/login/$" %} {% active request "^/help/$" %} {% active request "^/register/$" %} {% active request "^/thanks/$" %}'>
Connect<b class="caret"></b>
<ul class="dropdown-menu">
<li>Contact Us</li>
<li>Careers</li>
<li>Help</li>
<li>User Login</li>
</ul>
</li>
{% if request.get_full_path != "/" %}
<li class="dropdown">
Contact
</li>
{% else %}
<li class="dropdown">
Contact
</li>
{% endif %}
<!-- Navbar Search -->
<li class="hidden-xs" id="navbar-search">
<a href="#">
<i class="fa fa-search"></i>
</a>
<div class="hidden" id="navbar-search-box">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search">
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
</div>
</div>
</li>
</ul>
<!-- Mobile Search -->
<form class="navbar-form navbar-right visible-xs" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search">
<span class="input-group-btn">
<button class="btn btn-red" type="button">Search!</button>
</span>
</div>
</form>
</div><!--/.nav-collapse -->
</div>
</div>
</header>
<!-- / .navigation -->
{% block content %}
{% endblock %}
<!-- footer
================================================== -->
<footer>
<div class="container">
<div class="row">
<!-- Contact Us -->
<div class="col-sm-4">
<h4><i class="fa fa-map-marker text-red"></i> Contact Us</h4>
<p>Do not hesitate to contact us if you have any questions or feature requests:</p>
<p>
Omaha, NE 68154<br />
14301 FNB Parkway, Suite 100<br />
Phone: +1 402 957 1336<br />
Email: sales#multimechrd.com
</p>
</div>
<!-- Recent Tweets -->
{% load twitter_tag cache %}
{% cache 60 my_tweets %}
{% get_tweets for "multimechanics" as tweets limit 2 %}
<div class="col-sm-4">
<h4><i class="fa fa-twitter-square text-red"></i> Recent Tweets</h4>
{% for tweet in tweets %}
<div class="tweet">
<i class="fa fa-twitter fa-2x"></i>
<p>
{{ tweet.html|safe }}
</p>
</div>
{% endfor %}
</div>
{% endcache %}
<!-- Newsletter -->
<div class="col-sm-4">
<h4><i class="fa fa-envelope text-red"></i> Newsletter</h4>
<p>
Enter your e-mail below to subscribe to our free newsletter.
<br>
We promise not to bother you often!
</p>
<!--<form class="form" role="form" method="post" action="/newsletter{{ request.get_full_path }}">-->
<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">
<input type=hidden name="oid" value="00Di0000000fkHM">
<input type=hidden name="retURL" value="http://multimech2.azurewebsites.net/thanks/newsletter">
<input type=hidden name="lead_source" id="lead_source" value="Web">
<input type=hidden name="city" id="city" value="{{ip}}">
{% csrf_token %}
<div class="row">
<div class="col-sm-8">
<div class="input-group">
<label class="sr-only" for="subscribe-email">Email address</label>
<!--<input type="email" class="form-control" id="subscribe-email" placeholder="Enter your email">-->
<div class="fieldWrapper">{{ newsletter_form.email }}</div>
<span class="input-group-btn">
<button type="submit" class="btn btn-default" name="newsletter_form">OK</button>
</span>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</footer>
<!-- Copyright -->
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="copyright">
Copyright 2014 - MultiMechanics, LLC | All Rights Reserved
</div>
</div>
</div> <!-- / .row -->
</div> <!-- / .container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
{% compress js %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="{{ STATIC_URL }}js/bootstrap.min.js"></script>
<script src="{{ STATIC_URL }}js/scrolltopcontrol.js"></script>
<script src="{{ STATIC_URL }}js/lightbox-2.6.min.js"></script>
<script src="{{ STATIC_URL }}js/custom.js"></script>
{% endcompress %}
{% block otherfooter %}{% endblock %}
</body>
</html>
Here is my full child:
{% extends "base.html" %}
{% block content %}
<!-- Wrapper -->
<div class="wrapper">
<!-- Topic Header -->
<div class="topic">
<div class="container">
<div class="row">
<div class="col-sm-4">
<h3>Sign In</h3>
</div>
<div class="col-sm-8">
<ol class="breadcrumb pull-right hidden-xs">
<li>Home</li>
<li class="active">Log In</li>
</ol>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<div class="sign-form">
<div class="sign-inner">
<h3 class="first-child">Log In To Your Account</h3>
<hr>
<form role="form" action="" method="post">
{% csrf_token %}
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
{{form.username}}
</div>
<br>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
{{form.password}}
</div>
<div class="checkbox">
<!--<label>
<input type="checkbox"> Remember me
</label>-->
</div>
<button type="submit" class="btn btn-red" name="login_form">Submit</button>
<hr>
</form>
<p>Not registered? Create an Account.</p>
<div class="pwd-lost">
<div class="pwd-lost-q show">Lost your password? Click here to recover.</div>
<!--https://github.com/brutasse/django-password-reset-->
<div class="pwd-lost-f hidden">
<p class="text-muted">Enter your email address below and we will send you a link to reset your password.</p>
<form class="form-inline" role="form">
<div class="form-group">
<label class="sr-only" for="email-pwd">Email address</label>
<input type="email" class="form-control" id="email-pwd" placeholder="Enter email">
</div>
<button type="submit" class="btn btn-blue">Send</button>
</form>
</div>
</div> <!-- / .pwd-lost -->
</div>
</div>
</div>
</div> <!-- / .row -->
</div> <!-- / .container -->
</div> <!-- / .wrapper -->
{% endblock %}
Here is how the html is rendered:
<html lang="en">
<head>
<style type="text/css"></style>
</head>
<body style="">
<!-- base.html -->
<!--[if lt IE 8 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 8)|!(IE)]><!-->
<!--<![endif]-->
<meta charset="utf-8">
<!--<meta http-equiv="X-UA-Compatible" content="IE=edge">-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Navigation -->
<header>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/"><img src="/static/img/logo.png" alt="..."></a>
</div>
<div c
Here is how I render that page (but I use "render_to_response" for other templates and get the same result:
url(r'^about-us/', TemplateView.as_view(template_name="about-us.html"), name='about-us'),
Thanks in advance for the help.
I had similar problem.
Changing encoding of my files to "utf-8 without BOM" solved my issue.
(http://validator.w3.org/ may help u.)
Are you sure you're editing the correct "base.html" file? It appears that you may be editing a file that has the same name, but is in a different location. Also, are you using Javascript or something to populate your <navigation> </navigation> ? The rendered result does not match your base.html. If you are generating this navigation section using Javascript, this could be your culprit. If it was redacted for readability, then I would search for another instance of "base.html" or "about-us.html" in your project folder. You may find that you are editing the wrong file. Can you post the complete files "base.html" and "about-us.html" and the entire response?
Edit: I believe the issue may be in your "othermeta" block. You can see that the following lines render correctly:
<meta charset="utf-8">
<!--<meta http-equiv="X-UA-Compatible" content="IE=edge">-->
<meta name="viewport" content="width=device-width, initial-scale=1">
This is the point in your result that gets messy. It is also the point in your base when you call for {% block othermeta %}. Is there a reason you have that block with content inside? I'm not sure it is accepting the {% endblock %} correctly. If you want the block othermeta to be dynamic, you must have it in its own othermeta.html, which extends base.html as well. Then, you would change your code to simply:
{% block othermeta %} {% endblock %}
I think the error may be that you're trying to define the contents of this block in an extended template. I'm new at this like you, so I could be completely wrong, but try removing your othermeta block entirely for now and see if it helps.
Hope this helps!
After having the same issue in 2019, this was, as Majid Mobini said, due to the encoding of the source files by Visual Studio. As VS 2019 no longer has the Advanced Save options, my solution was to install the Fix File Encoding extension from the VS Marketplace and resave my files - which then placed the meta in the HEAD as expected.

Django view "not" passing context to template for dynamically filled bootstrap dropdown-menu

I have a base template that includes another template for a drop down menu. If I hard code items for the li tag, it works fine. If I try to pass the objects.all(), it doesn't fill. Here's my setup:
Model
class Category(models.Model):
name = models.CharField(max_length=40, blank=True)
def __unicode__(self): # Python 3: def __str__(self):
return self.name
View
def dropdown_cats(request):
cats = Category.objects.all()
return render(request, 'home/dropdown-cats.html', {'cats': cats})
home/base.html
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="{% static 'media/logo.png' %}">
<title>something.com</title>
<!-- Bootstrap core CSS -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="{% static 'dist/css/bootstrap.css' %}" type="text/css" media="screen" />
<link href="{% static 'dist/css/bootstrap.min.css' %}" rel="stylesheet" media="screen">
<!-- Custom CSS -->
<link href="{% static 'dist/css/custom.css' %}" rel="stylesheet" media="screen">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div id="site_wrapper" class="clearfix">
<!-- Above-Nav
================================================== -->
<div class="above-nav visible-desktop">
<div class="container" style="text-align:center;">
</div>
</div>
<!--END above-nav
================================================== -->
<!-- Navbar ==================================== -->
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href={{home}}>something.com</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
{% block navbar %}
{% endblock navbar %}
</ul>
</div>
</div><!--/.nav-collapse -->
</div><!-- navbar-inner -->
</div>
<!-- End Navbar =================================== -->
<!-- Start Grid layout ============================ -->
<!--<div class="container-fluid">-->
<div class="container-fluid">
<div class="row" style="text-align:center">
<div class="col-xs-3"><h2>My Most Recent Blogs</h2>
{% block left_side %}
{% endblock left_side %}
</div>
<div class="col-xs-6">
{% block middle %}
{% endblock middle %}
</div>
<div class="col-xs-3"><h2>Links</h2>
{% block right_side %}
{% endblock right_side %}
</div>
</div>
</div>
<!-- End Grid Layout ====================== -->
</div>
<!-- close the wrapper ====================== -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="{% static 'dist/js/bootstrap.min.js' %}"></script>
</body>
</html>
home/home.html
{% extends "home/base.html" %}
{% block navbar %}
<li>About</li>
<li>Contact</li>
<li class="dropdown">
Categories<span class="caret"></span>
{% include 'home/dropdown-cats.html' %}
</li>
<li>Previous Blogs</li>
<form class="navbar-form navbar-right" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
{% endblock navbar %}
{% block left_side %}
{% endblock left_side %}
{% block middle %}
{% endblock middle %}
{% block right_side %}
{% endblock right_side %}
home/dropdown-cats.html
<ul class="dropdown-menu">
{% if cats %}
{% for cat in cats %}
<li>{{ cat.name }}</li>
{% endfor %}
{% else %}
<li>BAD TEST</li>
{% endif %}
</ul>
If I test this by replacing the url tag in the for loop with something similar like GOOD TEST, there's still nothing inserted because cats is empty. All I get is one drop down entry "BAD TEST". I've used the shell to ensure Category.objects.all() returns the list as of categories as it should.
No idea how that Post statement got in the view there. Must have accidentally pasted it it. Anyways, I've excluded it from the view and included all my html for more details. I THINK I understand what you're saying Daniel, but I'm a bit confused. Isn't this how the tutorial modifies templates, by including it in the render parameters: render(request, 'template.html', dicts)
You don't really show enough information here, but I suspect you're getting confused about templates and views. Simply including another template doesn't "call" another view: the only thing that calls views is the URL handler. Templates themselves don't know anything about views.
If you want to dynamically add context for another template, you need to make it into a custom inclusion tag.
Replace the snippet in your Template with:
<ul class="dropdown-menu">
{% for cat in cats %}
<li>{{ cat.name }}</li>
{% empty %}
<li>BAD TEST</li>
{% endfor %}
</ul>
The above will definitely work as long as the cats queryset as set in your View code has 1 or more items. The for .. empty template tag used in the template code above is documented here.