Python 3.x 使用Django 3将变量从数据库传递到网站时遇到问题

Python 3.x 使用Django 3将变量从数据库传递到网站时遇到问题,python-3.x,django-models,django-views,django-generic-views,django-3.1,Python 3.x,Django Models,Django Views,Django Generic Views,Django 3.1,我还是django的新手,最近开始了一个有很多模型的项目。问题是,当我运行网站时,每当我想将一个变量从数据库传递到我的网站模板时,我看不到我的变量,它们只是显示为空。例如,如果在某个模型下有10个对象变量,当我使用for循环在有序列表(html)中迭代对象时,我在呈现页面上看到的都是空的项目符号。我迷路了,需要帮助 示例通知模型 from django.db import models from django.utils import timezone import da

我还是django的新手,最近开始了一个有很多模型的项目。问题是,当我运行网站时,每当我想将一个变量从数据库传递到我的网站模板时,我看不到我的变量,它们只是显示为空。例如,如果在某个模型下有10个对象变量,当我使用for循环在有序列表(html)中迭代对象时,我在呈现页面上看到的都是空的项目符号。我迷路了,需要帮助

示例通知模型

    from django.db import models
    from django.utils import timezone
    import datetime

    class Notice(models.Model):
        headline=models.CharField(max_length=150)
        notice_text=models.TextField()
        publication_date=models.DateTimeField('date published')

        def __str__(self):
            return self.headline
景色

    class Home_pageView(generic.ListView):
        template_name = 'Notices/home_page.html'
        context_object_name = 'notice_objects'

        def get_queryset(self):
            return Notice.objects.all()
    class Home_pageView(generic.ListView):
        template_name = 'Polls/home_page.html'
        context_object_name = 'latest_question_list'

        def get_queryset(self):
            return Question.objects.all()
模板

    {% if notice_objects %}
    <ul>
    {% for item in notice_objects %}
    <li><a href="{% url 'notices:detail' notice.id %}">{{ notice.notice_text }}</a></li>
    {% endfor %}
    </ul>
    {% else %}
    <p>No notices are available.</p>
    {% endif %}
    {% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
    <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
    {% else %}
    <p>No polls are available.</p>
    {% endif %}
模板

    {% if notice_objects %}
    <ul>
    {% for item in notice_objects %}
    <li><a href="{% url 'notices:detail' notice.id %}">{{ notice.notice_text }}</a></li>
    {% endfor %}
    </ul>
    {% else %}
    <p>No notices are available.</p>
    {% endif %}
    {% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
    <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
    {% else %}
    <p>No polls are available.</p>
    {% endif %}
{%if最新问题列表%}
    {最新问题列表%中的问题%
  • {%endfor%}
{%else%} 没有投票

{%endif%}

提前感谢大家的帮助。

在您的视图中,写一个非常细线的结构,如

 class Home_pageView(generic.ListView):
    template_name = 'Notices/home_page.html'
    model=Notice
问题列表视图的类似情况

在HTML模板中

 {% for something in object_list %}
 {{something.notice_text}}

在您的视图中,编写一个非常细线的结构,如

 class Home_pageView(generic.ListView):
    template_name = 'Notices/home_page.html'
    model=Notice
问题列表视图的类似情况

在HTML模板中

 {% for something in object_list %}
 {{something.notice_text}}

您可以使用中项目的引用
进行循环然后,您所指的不是
项目
,而是
注意事项
——这解释了您的空要点

将模板重写为:

<ul>
{% for notice in notice_objects %}
<li><a href="{% url 'notices:detail' notice.id %}">{{ notice.notice_text }}</a></li>
{% empty %}
<p>No notices are available.</p>
{% endfor %}
</ul>
    {%用于通知中的通知\u对象%}
  • {%empty%} 没有通知

    {%endfor%}

请注意,如果您使用
空的
标记,则不需要
if

您可以使用中项目的引用
进行循环然后,您所指的不是
项目
,而是
注意事项
——这解释了您的空要点

将模板重写为:

<ul>
{% for notice in notice_objects %}
<li><a href="{% url 'notices:detail' notice.id %}">{{ notice.notice_text }}</a></li>
{% empty %}
<p>No notices are available.</p>
{% endfor %}
</ul>
    {%用于通知中的通知\u对象%}
  • {%empty%} 没有通知

    {%endfor%}
请注意,如果使用
空标记,则不需要
if