0

我有一个项目,有一个应用程序,文章。我有一个页面列出所有文章,另一个页面显示所选文章及其评论。在我使用从南方迁移并进行一些更改之前,一切都很好,即在评论模型中我有一个字段

名称 = models.CharField(max_length=200)

并将其更改为:

first_name = models.CharField(max_length=200)
second_name = models.CharField(max_length=200)

现在,当我加载articles.html 页面时,一切正常,但是当我加载article.html 时,出现错误:

/articles/get/1/ 处的数据库错误

没有这样的列: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. 即使在控制台中,它也会给我与上述相同的错误。那么,我如何从给定的文章中获取所有评论?还是我在代码中犯了一些错误?任何帮助将不胜感激。谢谢你。

模型.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)

视图.py:

def article(request, article_id=1):
    return render_to_response('article.html',
        {'article': Article.objects.get(id=article_id) })

表格.py:

class CommentForm(forms.ModelForm):

    class Meta:
        model = Comment
        fields = ('first_name','second_name', 'body')

文章.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 %}
4

0 回答 0