django 如何在网页上显示多个渲染调用?

daolsyd0  于 2023-03-24  发布在  Go
关注(0)|答案(1)|浏览(101)

我刚开始使用Django框架,但不知道如何在我的网页上显示来自views python文件的多个render调用。
在url python文件中,我尝试添加一个新路径,并调用类似于createPunnettSquare的函数(运行良好)。我的项目运行良好,但我希望标题显示'punnett square',但只有'punnett'出现。
提前感谢任何帮助,因为这是我的第一篇文章。如果需要更多的信息,我可以明确地提供它。
webpage output

views.py

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

# Create your views here.
# request -> response
# request handler

def createPunnettSquare(request):
    genotype1 = "Aa"
    genotype2 = "Bb"
    l_offsprings = []
    punnett_square = [[""]*len(genotype1) for i in range(len(genotype2))]

    for i in range(len(genotype1)):
        for j in range(len(genotype2)):
            p_offspring = fill_array(request, j, i, genotype1, genotype2)
            l_offsprings.append(p_offspring)
    dy_offsprings = {"offsprings": l_offsprings}
    return render(request, 'hello.html', dy_offsprings)

def fill_array(request, j, i, genotype1, genotype2):
    offspring = genotype1[j] + genotype2[i]
    if offspring[0].isupper() == False:
        offspring = offspring[::-1]
    return offspring

def sayhi(request):
    return render(request, 'hello.html', {"name": "square"})
urls.py

from django.urls import path
from . import views

# URL configuration module (every app can have its own module)
urlpatterns = [
    path('hello/', views.createPunnettSquare),
    path('', views.sayhi)
]
<!DOCTYPE html>

<html>
    <head>

    </head>
    <style>
        .header1{
        text-align: center;
        }
        .header2{
        text-align: center;
        }
        .header3{
            text-align: center;
        }
        .monohybrid{
        margin: 20px auto;
        width:400px;
        height:400px;
        background-color:#fff;
        display:grid;
        grid-template-columns: 200px 200px;
        grid-row: auto auto;
        grid-column-gap: 20px;
        grid-row-gap: 20px;
        }
        .mBox{
        background-color:#3a3232;
        padding:20px;
        border-radius:10px;
        color:#fff;
        display:flex;
        align-items:center;
        justify-content:center;
        font-size:40px;
        font-family:sans-serif;
        }
    </style>
    <body>
        <h1 class="header1"> Punnett {{ name }} </h1>
        <h2 class="header2"> Monohybrid Crosses</h2>
        <!-- <form action="/action_page.php">
            <label for="fname">P1 Genotype: </label>
            <input type="text" id="p1geno" name="p1geno"><br><br>
            <label for="lname">P2 Genotype: </label>
            <input type="text" id="p2geno" name="p2geno"><br><br>
            <input type="submit" value="Submit">
          </form> -->
        <div class="monohybrid">
            {% for i in offsprings %}
            <div class="mBox">{{ i }} </div>
            {% endfor %}
        </div>

    </body>
</html>
xdnvmnnf

xdnvmnnf1#

render()函数的第三个参数是context变量,你可以在其中提供你想要在模板中使用的所有变量。所以把所有的东西都放在第一个视图函数中:

context = {
    "offsprings": l_offsprings,
    "name": "square",
}
return render(request, 'hello.html', context)

相关问题