0

我是 Django 的新手。我有任务,我有星座网站,有12个星座水瓶座,白羊座等......在Aquarius.html水瓶座爱情,水瓶座金融......水瓶座的内容(我不想复制粘贴Aquarius.html并创建另一个爱情文件然后显示)怎么办?例如,当用户打开 Aquarius 我想显示

 {% for aqua in aquas %}
                       {% if forloop.last %}
                        Aqua Content text{{aqua.body_text}}
                       {% endif %}
                       {% endfor %}

打开水瓶座恋爱时(点击恋爱按钮)显示恋爱内容

 {% for Love in Loves %}
                           {% if forloop.last %}
                           love Content text  {{aqua.love_body_text}}
                           {% endif %}
                           {% endfor %}

怎么能做到这一点?如果别的 ?

4

1 回答 1

0

我认为你的模型是这样的:

    class ZodiacSign(models.Model):
        name = models.CharField(...)
        body_text = models.TextField(...)
        .
        .
        .
        love_content = models.TextField(...) or models.ForeignKey(...) or models.ManytoManyField(...)

您的网址可能是这样的:

    from zodiac.views import showZodiacSign
    urlpatterns = [
        .
        .
        path("zodiacSign/<int:zodiacSignPk>", showZodiacSign, name = "showZodiacSign"),
        .
        .
    ]

我认为你的观点是这样的:

    def showZodiacSign(request, zodiacSignPk):
        zodiacSign = ZodiacSign.objects.get(pk = zodiacSignPk)
        return render(request, "zodiacSign.html", { "zodiacSign", zodiacSign })

和你的 html 文件 ZodiacSign.html。如果我理解正确,您只需要一个名为 ZodiacSign.html 的 html 文件:

   {{ zodiacSign.name }} Content Text : {{ zodiacSign.body_text }}

   <button onclick="showContent('loveContent');">Show Love Content</button>
    <style>
        .zodiacContent{
        /*this->*/    display: none;
        /*or this->    visibility: hidden;*/
        }
    </style>
    <div id = "loveContent" class="zodiacContent">
        {{ zodiacSign.love_content }} {# if you used models.TextField() for love content #}
    </div>
    <script>
        function showContent(contentBlockId) {
            let content = document.getElementById(contentBlockId);
            content.classList.toggle("zodiacContent");
        }
    </script>
于 2020-09-13T13:23:40.820 回答