【问题标题】:i need help beacause django with doesnt really work for me我需要帮助,因为 django with 并不适合我
【发布时间】:2021-05-05 22:56:00
【问题描述】:

我正在处理的学校项目有问题。当我运行代码并转到我创建的“随机页面”链接时,没有任何反应。尝试了一下后,我认为问题在于似乎找不到 {{}} 中的任何内容。视图.py:

from django.shortcuts import render
from django.http import HttpResponse
from . import util
import random

def index(request):
    return render(request, "encyclopedia/index.html", {
        "entries": util.list_entries()
    })
    random_page = random.choice(entries)
def CSS(request):
    return render(request, "encyclopedia/css_tem.html", {
        "article_css": "css is slug and cat"
    })
def Python(request):
    return render(request, "encyclopedia/python_tem.html", {
        "article_python": "python says repost if scav"
    })
def HTML(request):
    return render(request, "encyclopedia/HTML_tem.html", {
        "article_HTML": "game theory: scavs are future humans"
    })
def Git(request):
    return render(request, "encyclopedia/Git_tem.html", {
        "article_Git": "github is git"
    })
def Django(request):
    return render(request, "encyclopedia/Django_tem.html", {
        "article_Django": "this is a framework"
    })

layout.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>{% block title %}{% endblock %}</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
        <link href="{% static 'encyclopedia/styles.css' %}" rel="stylesheet">
    </head>
    <body>
        <div class="row">
            <div class="sidebar col-lg-2 col-md-3">
                <h2>Wiki</h2>
                <form>
                    <input class="search" type="text" name="q" placeholder="Search Encyclopedia">
                </form>
                <div>
                    <a href="{% url 'index' %}">Home</a>
                </div>
                <div>
                    Create New Page
                </div>
                <div>
                    <a href = "http://127.0.0.1:8000/{{random_page}}">Random Page</a>
                </div>
                {% block nav %}
                {% endblock %}
            </div>
            <div class="main col-lg-10 col-md-9">
                {% block body %}
                {% endblock %}
            </div>
        </div>

    </body>
</html>

【问题讨论】:

    标签: python django


    【解决方案1】:

    在选择random_page 之前,您的index() 方法returns,因此代码永远不会运行。您的文件名与您的代码不匹配,但本质上您需要将随机页面名称传递到索引页面的上下文中。

    查看:

    def index(request):
        random_page = random.choice(entries)
        return render(request, "encyclopedia/index.html", {
            "entries": util.list_entries(),
            "random_page": random_page,
        })
    

    “百科全书/index.html”

    [...]
     <div>
       <a href = "http://127.0.0.1:8000/{{random_page}}">Random Page</a>
     </div>
    [...]
    

    【讨论】:

    • 我设法修复它:def index(request): entries = util.list_entries() random_page = random.choice(entries) return render(request, "encyclopedia/index.html", { "entries": util.list_entries(), "random_page": random_page }) 感谢您的帮助
    • @Tryjis 仅供参考,您不需要 entries = util.list_entries() 行,因为您已经在上下文字典 ({ "entries": util.list_entries(), ... }) 中分配了它。
    猜你喜欢
    • 2022-01-10
    • 2022-01-04
    • 2014-03-02
    • 1970-01-01
    • 2020-10-13
    • 2021-02-07
    • 1970-01-01
    • 2021-04-25
    • 2016-07-14
    相关资源
    最近更新 更多