2017-02-19 20 views
1

私のDjangoプロジェクトはTypeErrorを返しています: 'module'オブジェクトは反復可能ではありません。'module'オブジェクトは反復可能ではありません

私はこのタイプの質問がすでにコミュニティで尋ねられていることを知っていますが、これまでの質問のいずれも私の問題を解決できませんでした。

おそらく、私は初心者のPythonとDjangoを習得しているので、基本的なことは分かりません。誰も私がこの問題を解決するのを助けることができますか?

次のようにモデルを作成しました。続き

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 

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> 

答えて

2

あるrenderの署名がある:あなたが、しかし、あなたにそれを呼び出す

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

indexとW:あなたが通過

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

dict第三の位置引数として、エラーが発生している、(十分に悪い)とrequestとして(あるべきdict)あなたはcontextの名前で変数を渡しますあなたがあなたの0123で同じミスを犯した

from django.template import context 

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

に変更し、それを経由して輸入しているモジュールが表示されます。

+0

非常に高く評価されました!それは完璧に私の問題を解決! –