我的Django项目正在返回一个TypeError:'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>
答案 0 :(得分:2)
render
的签名是:
render(request, template_name, context=None, content_type=None, status=None, using=None)
但是,您可以在index
视图中调用它:
return render({'all_articles' : all_articles, 'message' : 'Write something!'},
'blog/index.html', context)
你将dict
传递给request
(足够糟糕)并导致错误,作为第三个位置参数(应该是dict
)你传递一个变量名称context
,它是您通过
from django.template import context
将其更改为
return render(request, 'blog/index.html',
context={'all_articles': all_articles, 'message': 'Write something!'})
您在submit
视图中犯了同样的错误。