Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'module' object is not iterable

Tags:

python

django

My Django project is returning a TypeError: 'module' object is not iterable.

I know this type of question is already being asked in community, but none of previous questions could fixed my issue.

perhaps I don't understand something basic, as I'm a novice who freshly learning Python and Django. does anyone can help me to solve this issue?

I created a model as following.

from django.db import models

# Create your models here.
class Article(models.Model):
    content = models.CharField(max_length=200)
    written_date = models.DateTimeField('date written')
    def __unicode__(self):
        return self.content

Following is view.py

# Create your views here.
from blog.models import Article  # Article data models
from django.shortcuts import render # shortcuts to call template
from django.http import HttpResponseRedirect # Redirection Module
from django.template import context
from django.utils import timezone # time Module

# blog.views.index
# retrieve all content and display in reverse of order of written_date
# call the template after sorting.
def index(request):
    all_articles = Article.objects.all().order_by('-written_date')
    return render({'all_articles' : all_articles, 'message' : 'Write something!'},
        'blog/index.html', context)

# blog.views.submit
# Receive POST request submitted from user FORM, save the request
# redirect user to index.html

def submit(request):
    try:
        cont = request.POST['content']
    except (KeyError):
        return render({'all_articles' : all_articles, 'message' : 'Failed to read content'},
                'blog/index.html', context)
    else:
        article = Article(content=cont, written_date=timezone.now())
        article.save()
        return HttpResponseRedirect('/blog')

# blog.views.remove
# delete content which has primary key matched to article_id
# redirect user after deleting content
def remove(request, article_id):
    article = Article.objects.get(pk=article_id)
    article.delete()
    return HttpResponseRedirect('/blog')

Index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>One Line Blog</title>
    <link rel="stylesheet" href="{{ STATIC_URL }}styles/blog.css" type="text/css">
  </head>
  <body>
    <div id="header">
      <h2>One Line Blog</h2>
    </div>
    <div id="writer">
      <div>
        {% if message %}<p><strong>{{ message }}</strong></p>{% endif %}
      </div>
      <form action="/blog/submit" method="post">
        {% csrf_token %}
        <input type="text" max-length=200 style="width:500px;" name="content">
        <input type="submit" value="Submit">
      </form>
    </div>
    <div>
    {% if all_articles %}
      <table>
        <tr>
          <th>No. </th>
          <th width="300px">Content</th>
          <th>Date</th>
          <th>Delete</th>
        </tr>
        { % for article in all_articles %}
        <tr>
          <td>{{ article.id }}</td>
          <td>{{ article.content }}</td>
          <td>{{ article.written_date }}</td>
          <td align="center"><a href="/blog/{{ article.id }}/remove">[x]</a></td>
        </tr>
        { % endfor %}
      </table>
      {% else %}
      <p>No articles available</p>
      {% endif %}
    </div>
  </body>
</html>
like image 911
Yunjae Oh Avatar asked Feb 19 '17 00:02

Yunjae Oh


People also ask

How do I fix this object is not iterable?

The Python "TypeError: 'function' object is not iterable" occurs when we try to iterate over a function instead of an iterable (e.g. a list). To solve the error, make sure to call the function, e.g. my_func() if it returns an iterable object.

Why is float not iterable in python?

Conclusion # The Python "TypeError: 'float' object is not iterable" occurs when we try to iterate over a float or pass a float to a built-in function like, list() or tuple() . To solve the error, use the range() built-in function to iterate over a range, e.g. for i in range(int(3.0)): .

How do I fix TypeError module object is not Subscriptable?

The Python "TypeError: 'module' object is not subscriptable" occurs when we import a module as import some_module but use square brackets to access a specific key. To solve the error, use dot notation to access the specific variable or import it directly.


1 Answers

The signature of render is:

render(request, template_name, context=None, content_type=None, status=None, using=None)

You, however, call it in your index view:

return render({'all_articles' : all_articles, 'message' : 'Write something!'},
    'blog/index.html', context)

where you pass a dict as request (bad enough) and, which causes the error, as third positional argument (which should be a dict) you pass a variable by the name of context which is a module that you have imported via

from django.template import context

Change it to

return render(request, 'blog/index.html',
             context={'all_articles': all_articles, 'message': 'Write something!'})

You made the same mistake in your submit view.

like image 91
user2390182 Avatar answered Sep 20 '22 22:09

user2390182