模型问题无效

时间:2018-06-10 15:23:29

标签: python django

我正在关注一个教程并尝试使网站类似于堆栈溢出,允许人们发布他们的问题。但就目前而言,我刚刚创建的问题没有显示在我的HTML页面上,因为其他HTML元素运行正常。我设置了'DEBUG = True'在setting.py上,但我没有得到任何反馈。我想我的HTML页面或者models.py可能有问题。但我无法弄明白为什么。有人可以帮忙吗?

models.py

class Issue(models.Model):
    STATUS_CHOICES = (
        ('draft','Draft'),
        ('published','Published'),
    )
    project = models.ForeignKey(Project,on_delete=models.PROTECT)
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250,unique=True)
    content = models.TextField()
    author = models.ForeignKey(User,on_delete=models.PROTECT)
    published = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now = True)
    status = models.CharField(max_length=9,choices= STATUS_CHOICES,default='draft')

    def save(self,*args,**kwargs):
        self.slug = slugify(self.title)
        super(Issue,self).save(*args,**kwargs)

    def get_absolute_url(self):
        return reverse('blog:issue_detail',args=[self.slug])

    def __str__(self):
        return self.title

view.py

def list_of_issue(request):
    issue = Issue.objects.filter(status='published')
    paginator = Paginator(issue,10)
    page = request.GET.get('page')
    try:
        issues = paginator.page(page)
    except PageNotAnInteger:
        issues = paginator.page(1)
    except EmptyPage:
        issues = paginator.page(paginator.num_pages)            
    template = 'blog/issue/list_of_issue.html'
    return render(request,template,{'issues':issues,'page':page})

list_of_issue.html

{% extends 'blog/issue/base.html' %}

{% block description %}This is the description{% endblock %}
{% block title %}List of blog Issue{% endblock %}

{% block content %}
    <div class="row">
        <div class="col-md-8">
            <div class="page-header"><h1>Our lastest issues</h1></div>
            <button class="btn btn-default"><a href="{% url 'blog:new_issue'%}">New issues</a></button>
     {% for Issue in Issues %}
         <h2><a href="{{Issue.get_absolute_url}}">{{ Issue.title }}</a></h2>
         <small>Written by {{ Issue.author }} on {{ Issue.published}} in <a href="{{Issue.project.get_absolute_url}}">{{Issue.project}}</a></small>
         <hr>
         {{ Issue.content | truncatewords:40 | linebreaks}}
     {% endfor %}
     {% include 'blog/paginator.html' with page=Issues%}
    </div>
    <div class="col-md-4">
        <div class="page-header">
     <h1>Use info/project info</h1>
     </div>
    </div>
    </div>     
{% endblock %}

1 个答案:

答案 0 :(得分:1)

嗯,您的HTML模板中有拼写错误,以下是您在上下文中返回的内容issues请注意所有字母均为小写

return render(request,template,{'issues':issues,'page':page})

在模板中,您使用大写字母迭代Issues

{% for Issue in Issues %}

将其更改为

{% for Issue in issues %}

我还建议您将变量issue重命名为issues

def list_of_issue(request):
    issue issues = Issue.objects.filter(status='published')
    paginator = Paginator(issue,10)
    page = request.GET.get('page')
    try:
        issues = paginator.page(page)
    # rest of the code

由于您不想要分页,为了避免NameError,查询集始终为issues,因为它是在此处发送给模板的名称{{ 1}}