1

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: http://dpaste.com/3D1B7MG

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">
<head>
    <meta charset="UTF-8">
    <style></style>
<title></title>
</head>
<body>
</body>
</html>

base.html

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
   <HEAD>
       <link rel="stylesheet"     href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
       <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
       <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>

</HEAD>

   <BODY>
   {% 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">
    <ul>
        {% if user.is_authenticated %}
            <li>Welcome,</li>
            <li><a href="{% url 'profile' %}">{{ user.username }}</a></li>
            <li>|</li>
            <li><a href="{% url 'logout' %}">Log Out</a></li>
        {% else %}
            <li>Please</li>
            <li><a data-toggle="modal" data-target="#modal-login" href="">log in</a></li>
            <li>or</li>
            <li><a data-toggle="modal" data-target="#modal-register" href="">sign up</a></li>
        {% endif %}
    </ul>
    </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>
          </ul>
        </li>
      </ul>
  </div><!-- /.container-fluid -->
</nav>
<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>
                        </button>
                    </span>
            </div>
            </form>
    </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="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">    </script>
    <script  src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> </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 %}
   </BODY>
</HTML>

views.py

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})
    else:
        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')

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

    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>
                    <div class="modal-body">
                        {% csrf_token %}
                        {{ authenticationform|crispy }}
                    </div>
                    <div class="modal-footer">
                        <input type='submit' class="btn btn-primary" value="Log In" />
                    </div>
                </form>
            </div>
        </div>
</div>

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>
                    <div class="modal-body">
                        {% csrf_token %}
                        {{ registrationform|crispy }}
                    </div>
                    <div class="modal-footer">
                        <input type='submit' class="btn btn-primary" value="Register" />
                    </div>
                </form>
            </div>
        </div>
</div>
3
  • why are you extending base.html anyway? You are not making use of anything in base in all_posts Commented Jul 20, 2016 at 2:11
  • Show register.html and login.html templates and urls.py, please. Commented Jul 20, 2016 at 2:52
  • I added login.html and register.html. they are basically just modals. Commented Jul 20, 2016 at 8:20

2 Answers 2

1

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>

and

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

I ran into this error while doing the Django tutorial.

The core problem was in my app's urls.py, in urlpatterns, with the second argument here:

    path('', views.index, views.IndexView.as_view(), name='index'),

It should have been:

    path('', views.IndexView.as_view(), name='index'),

Not sure how the extra views.index snuck in there but it was causing the problem. (as to why that exact error occurs - not going to dig too deep)

I believe this is preferable to deleting the {% url '...' id %} call and hard-coding its result as advised in the solution above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.