我有一个项目,有一个应用程序,文章。在那里,我有一个页面列出所有文章,另一个页面显示所选文章及其评论。在我从南方使用迁移之前一切都很好,并进行了一些更改,即在评估模型之前我有一个字段
name = models.CharField(max_length = 200)
并将其更改为:
first_name = models.CharField(max_length=200)
second_name = models.CharField(max_length=200)
现在当我加载我的articles.html页面时,everthings没问题,但是当我加载article.html时,我收到错误:
/ articles / get / 1 /
中的DatabaseError没有这样的专栏:article_comment.first_name
模板渲染期间出错
在模板C:\ Users \ Robin \ web \ django_test \ article \ templates \ article.html中,第21行出错
在第21行的article.html中:
{% if article.comment_set.all %}
我认为问题出在comment_set.all
上。即使在控制台中它也给出了与上面相同的错误。那么,我如何得到给定aritcle的所有评论?或者我在代码中犯了一些错误?任何帮助将不胜感激。谢谢。
Models.py:
from django.db import models
from time import time
def get_upload_file_name(instance, filename):
return "uploaded_files/%s_%s" % (str(time()).replace('.','_'), filename)
class Article(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
pub_date = models.DateTimeField('date published')
likes = models.IntegerField(default=0)
thumbnail = models.FileField(upload_to=get_upload_file_name)
def __unicode__(self):
return self.title
class Comment(models.Model):
first_name = models.CharField(max_length=200)
second_name = models.CharField(max_length=200)
body = models.TextField()
pub_date = models.DateTimeField('date published')
article = models.ForeignKey(Article)
views.py:
def article(request, article_id=1):
return render_to_response('article.html',
{'article': Article.objects.get(id=article_id) })
forms.py:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('first_name','second_name', 'body')
article.html:
{% extends "base.html" %}
{% block sidebar %}
<ul>
<li><a href="/articles/all">Articles</a></li>
</ul>
{% endblock %}
{% block content%}
<h1>{{article.title}}</h1>
<p>{{article.body}}</p>
{% if article.thumbnail %}
<p><img src="/static/assets/{{ article.thumbnail }}" width="200"/></p>
{% endif %}
<p>{{ article.likes }} people liked this article.</p>
<p><a href="/articles/like/{{ article.id }}">Like</a></p>
<h2>Comments</h2>
{% if article.comment_set.all %}
{% for c in article.comment_set.all %}
<p>{{ c.name }}: {{ c.body }}</p>
{% endfor %}
{% else %}
<p>No comment</p>
{% endif %}
<p><a href="/articles/add_comment/{{ article.id }}">Add comment</a></p>
{% endblock %}