ngx311 ngx311 - 1 year ago 166
HTML Question

Django - 'function' object is not iterable - Error during template rendering

So, I've been trying to create a user login. When I click submit on my login form, I get this error:

Exception Type: TypeError
Exception Value: 'function' object is not iterable

Here's the full Traceback:

So, If I'm reading the Traceback correctly, the problem is in the {% extends "base.html" %} line of all_poss.html. So would that would mean the problem is actually inside base.html? or in the view that controls all_posts?

My all_posts.html

{% extends "base.html" %}
{% load staticfiles %}

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">


{% load staticfiles %}
{% load crispy_forms_tags %}

<link rel="stylesheet" href="//">
<link rel="stylesheet" href="//">
<link rel="stylesheet" href="{% static 'css/base.css' %}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
<TITLE>{% block title %}{% endblock %}</TITLE>


{% block content %}
<div class="navbar-wrapper">
<div class="post_button" style="width:58px; margin:0 auto;">
<a href="#" class="btn btn-custom blue">Submit a Post</a>
</div> <!-- /.post_button-->
<div class="log_bar">
{% if user.is_authenticated %}
<li><a href="{% url 'profile' %}">{{ user.username }}</a></li>
<li><a href="{% url 'logout' %}">Log Out</a></li>
{% else %}
<li><a data-toggle="modal" data-target="#modal-login" href="">log in</a></li>
<li><a data-toggle="modal" data-target="#modal-register" href="">sign up</a></li>
{% endif %}
</div><!-- /.log_bar -->
<nav class="navbar navbar-fixed-left navbar-static-top">
<div class="container-fluid">
<!-- Collect the nav links, forms, and other content for toggling -->
<ul class="nav navbar-nav ">
<li class="active"><a href="/">Home <span class="sr-only">(current)</span></a></li>
<li><a href="">All</a></li>
<li><a href="">New</a></li
<li class="dropdown">
<a href="" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Top<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="">hour</a></li>
<li><a href="">24 hrs</a></li>
<li><a href="">week</a></li>
<li><a href="">month</a></li>
<li><a href="">year</a></li>
<li><a href="">beginning of time</a></li>
<li role="separator" class="divider"></li>
<li><a href="">Custom Search</a></li>
</div><!-- /.container-fluid -->
<div id="side_bar">
<form class="navbar-form navbar-static-top navbar-right" role="search" id="navBarSearchForm">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search">
<span class="input-group-btn">
<button type="submit" class="btn btn-default" id="search_btn">
<span class="glyphicon glyphicon-search"></span>
</div><!-- /.side-bar -->
<button class="btn-block" id='hideshow' value='hide/show' style="display: block; height: 100%;"></button>

{% include 'register.html' %}
{% include 'login.html' %}

<script src=""> </script>
<script src=""> </script>
<script type="text/javascript" src="{{ STATIC_URL }} /static/jquery.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }} /static/jquery.leanModal.js"></script>

{% endblock %}

def login(request):
Log in view
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
user = authenticate(username=request.POST['username'], password=request.POST['password'])
if user is not None:
if user.is_active:
django_login(request, user)
return render(request, 'all_posts.html', {'user': request.user})
form = AuthenticationForm()
return render_to_response('login.html', {
'authenticationform': form,
}, context_instance=RequestContext(request))

def all_posts(request):
post_list = TextPost.objects.all().order_by('-score'))
paginator = Paginator(post_list, 100) # Show 100 contacts per page
registrationform = RegistrationForm(request.POST or None)
authenticationform = AuthenticationForm(request.POST or None)

page = request.GET.get('page')

posts =
except PageNotAnInteger:
# If page is not an integer, deliver first page.
posts =
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
posts =

return render(request, 'all_posts.html', {'posts': posts, 'registrationform': registrationform, 'authenticationform': authenticationform, 'user': request.user})

Edit 1: login.html login.html and register.html are identical modals, one with a login form and the other with the registration one.

{% load staticfiles %}
{% load crispy_forms_tags %}

<div class="modal" id="modal-login">
<div class="modal-dialog">
<div class="modal-content">
<form enctype="multipart/form-data" method="post" action="login/">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h3 class="modal-title">Log In</h3>
<div class="modal-body">
{% csrf_token %}
{{ authenticationform|crispy }}
<div class="modal-footer">
<input type='submit' class="btn btn-primary" value="Log In" />

Edit 2: register.html

{% load staticfiles %}
{% load crispy_forms_tags %}

<div class="modal" id="modal-register">
<div class="modal-dialog">
<div class="modal-content">
<form enctype="multipart/form-data" method="post" action="register/">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h3 class="modal-title">Register</h3>
<div class="modal-body">
{% csrf_token %}
{{ registrationform|crispy }}
<div class="modal-footer">
<input type='submit' class="btn btn-primary" value="Register" />

Answer Source

Alright, I was able to solve it.

The problem was apparently in my base.html where I had:

<li><a href="{% url 'profile' %}">{{ user.username }}</a></li>


<li><a href="{% url 'logout' %}">Log Out</a></li>

and I just changed the href="{% %}" tags to "profile/" and "logout/" respectively.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download