Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django模板逻辑_Django_Templates - Fatal编程技术网

Django模板逻辑

Django模板逻辑,django,templates,Django,Templates,所以我现在正和Django打交道 我想做一件相对保守的事。我有一个这样的模型: class Article(models.Model): id = models.IntegerField(primary_key=True) volnumber = models.IntegerField(db_column='volNumber') title = models.TextField() keywords = models.TextField(blank=True)

所以我现在正和Django打交道

我想做一件相对保守的事。我有一个这样的模型:

class Article(models.Model):
    id = models.IntegerField(primary_key=True)
    volnumber = models.IntegerField(db_column='volNumber')
    title = models.TextField()
    keywords = models.TextField(blank=True)
    start_page = models.IntegerField(null=True, blank=True)
    end_page = models.IntegerField(null=True, blank=True)
    author_1 = models.TextField(blank=True)
    author_2 = models.TextField(blank=True)
    author_3 = models.TextField(blank=True)
    author_4 = models.TextField(blank=True)
    author_5 = models.TextField(blank=True)
    author_6 = models.TextField(blank=True)
    author_7 = models.TextField(blank=True)
    author_8 = models.TextField(blank=True)
    author_9 = models.TextField(blank=True)
    author_10 = models.TextField(blank=True)
我认为:

def index(request):
    article_list = Article.objects.all()
    volume_list = Volume.objects.all()
    auth_list = ['author_1', 'author_2', 'author_3', 'author_4', 'author_5', 'author_6', 'author_7', 'author_8', 'author_9', 'author_10', ]

    return render_to_response('index.html', {
            'article_list': article_list,
            'volume_list': volume_list,
            'auth_list' : auth_list,
    })
我想迭代第一篇文章的作者,并消除模板中列表的空条目:

<ul class="articleList">
    {% for article in article_list %}
        <li>
        {% for author in auth_list %}
            <i>{{ article."author" }}</i>
        {% endfor %}

        {{ article.title }}{{ article }}
        </li>
    {% endfor %}
 </ul>
<ul class="articleList">
    {% for article in article_list %}
        <li>
        {% for author in auth_list %}
            {% if author %}
            <i>{{ author }}</i>
            {% endif %}
        {% endfor %}

        {{ article.title }}{{ article }}
        </li>
    {% endfor %}
 </ul>
    {文章列表%中的文章为%0}
  • {auth_list%中的作者为%s} {{文章.作者}} {%endfor%} {{article.title}{{article}}
  • {%endfor%}
显然,这是行不通的,我不确定模板是否适合这种逻辑,但我希望从中可以清楚地看出我想要实现什么


非常感谢您的帮助。

我不确定您是否希望以这种方式设计作者列表。如果您想要多个作者,请考虑为作者使用新模型,然后在文章类中使用MyTyMyFielfield将文章链接到作者。然后,您可以在模板中循环遍历作者,如下所示:

{% for author in article.authors.all %}
    <!-- blah blah -->
{% endfor %}
以及调整后的文章模型:

class Article(models.Model):
    # add this line
    authors = models.ManyToManyField(Author)

现在,您不需要在视图代码中返回
auth\u列表。

我不确定您是否希望以这种方式设计作者列表。如果您想要多个作者,请考虑为作者使用新模型,然后在文章类中使用MyTyMyFielfield将文章链接到作者。然后,您可以在模板中循环遍历作者,如下所示:

{% for author in article.authors.all %}
    <!-- blah blah -->
{% endfor %}
以及调整后的文章模型:

class Article(models.Model):
    # add this line
    authors = models.ManyToManyField(Author)

现在,您不需要在视图代码中返回
auth_列表

如果您不想更改数据模型(尽管我强烈建议您遵循@andrew lee的建议),则不允许在模板中按名称调用属性。你应该做:

# in the view
return render_to_response('index.html', {
        'article_list': article_list,
        'volume_list': volume_list,
        'auth_list' : [getattr(article, name) for name in auth_list],
})

# in the template
    {% for author in auth_list %}
        <i>{{ author }}</i>
    {% endfor %}
视图中的
#
返回render\u to\u响应('index.html'{
“文章列表”:文章列表,
“卷列表”:卷列表,
“授权列表”:[getattr(article,name)表示授权列表中的名称],
})
#在模板中
{auth_list%中的作者为%s}
{{作者}
{%endfor%}

如果您不想更改数据模型(尽管我强烈建议您遵循@andrew lee的建议),则不允许在模板中按名称调用属性。你应该做:

# in the view
return render_to_response('index.html', {
        'article_list': article_list,
        'volume_list': volume_list,
        'auth_list' : [getattr(article, name) for name in auth_list],
})

# in the template
    {% for author in auth_list %}
        <i>{{ author }}</i>
    {% endfor %}
视图中的
#
返回render\u to\u响应('index.html'{
“文章列表”:文章列表,
“卷列表”:卷列表,
“授权列表”:[getattr(article,name)表示授权列表中的名称],
})
#在模板中
{auth_list%中的作者为%s}
{{作者}
{%endfor%}

根据已经说过的内容,更改模型是最有意义的,但是如果您希望保持模型不变,并且只显示已填写的作者字段列表,您可以在模板中执行以下操作:

<ul class="articleList">
    {% for article in article_list %}
        <li>
        {% for author in auth_list %}
            <i>{{ article."author" }}</i>
        {% endfor %}

        {{ article.title }}{{ article }}
        </li>
    {% endfor %}
 </ul>
<ul class="articleList">
    {% for article in article_list %}
        <li>
        {% for author in auth_list %}
            {% if author %}
            <i>{{ author }}</i>
            {% endif %}
        {% endfor %}

        {{ article.title }}{{ article }}
        </li>
    {% endfor %}
 </ul>
    {文章列表%中的文章为%0}
  • {auth_list%中的作者为%s} {%if author%} {{作者} {%endif%} {%endfor%} {{article.title}{{article}}
  • {%endfor%}

根据已经说过的内容,更改模型是最有意义的,但是如果您希望保持模型不变,并且只显示已填写的作者字段列表,您可以在模板中执行以下操作:

<ul class="articleList">
    {% for article in article_list %}
        <li>
        {% for author in auth_list %}
            <i>{{ article."author" }}</i>
        {% endfor %}

        {{ article.title }}{{ article }}
        </li>
    {% endfor %}
 </ul>
<ul class="articleList">
    {% for article in article_list %}
        <li>
        {% for author in auth_list %}
            {% if author %}
            <i>{{ author }}</i>
            {% endif %}
        {% endfor %}

        {{ article.title }}{{ article }}
        </li>
    {% endfor %}
 </ul>
    {文章列表%中的文章为%0}
  • {auth_list%中的作者为%s} {%if author%} {{作者} {%endif%} {%endfor%} {{article.title}{{article}}
  • {%endfor%}

而且,使用新模型,按作者组织文章也变得容易多了。嘿,谢谢你的输入。我确实意识到这些模型看起来不太好看,尽管我认为它们是来自现有数据的遗留模型。但我认为我的努力是值得的。感谢您快速的回答-这一切似乎都很简单,因为模型设计正确。干杯!有了新的模式,按作者组织文章也容易多了。嘿,谢谢你的投入。我确实意识到这些模型看起来不太好看,尽管我认为它们是来自现有数据的遗留模型。但我认为我的努力是值得的。感谢您快速的回答-这一切似乎都很简单,因为模型设计正确。干杯!你确定你的文章有足够的作者字段吗?你可能需要更多!你确定你的文章有足够的作者字段吗?你可能需要更多!