表单未显示在Django网站上

时间:2020-01-27 07:01:09

标签: django django-forms

我刚刚开始学习Django,但遇到了一些障碍,希望有人可以提供帮助。

问题: 我的表单未显示在我的主要HTML模板(ingredior.html)中,但是该表单将显示在我的测试HTML模板(test.html)中,该模板只是带有{{form1}}标签的模板HTML文件。我正在手动更改views.py文件中的"return render(request, 'ingredior/ingredior.html', context)",以测试两个不同的HTML模板。

ingredior.html test.html

问题: 更改后,为什么表单在test.html中而不在ingredior.html文件中起作用?

请记住,我正在将代码行"return render(request, 'ingredior/ingredior.html', context)""return render(request, 'ingredior/test.html', context)"交换以在两者之间进行测试。

代码---------

Forms.py

from django import forms

class UserInput(forms.Form):
    base_search_ingredient = forms.ChoiceField(choices=[('vegan', 'Vegan'), ('vegatarian', 'Vegatarian')])

views.py

from django.shortcuts import render
import requests
from .local_api_key import Key
from .forms import UserInput


def index (request):

    app_id = Key.app_id
    app_key = Key.app_key
    search_from = 0
    search_to = 100
    form1 = UserInput()
    test = []

    if request.POST.get('search'):
        test2 = request.POST.get('search')
        intolerance = test2
        url = requests.get(f"https://api.edamam.com/search?q={intolerance}&excluded=egg&excluded=beef&excluded=dairy&excluded=tomato&excluded=cherry%20tomatoes&excluded=rice&excluded=corn&excluded=soy&excluded=onion&from={search_from}&to={search_to}&health={intolerance}&app_id={app_id}&app_key={app_key}").json()
        recipe_length = (len(url['hits']))
    else:
        intolerance = 'vegan'
        url = requests.get(f"https://api.edamam.com/search?q={intolerance}&excluded=egg&excluded=beef&excluded=dairy&excluded=tomato&excluded=cherry%20tomatoes&excluded=rice&excluded=corn&excluded=soy&excluded=onion&from={search_from}&to={search_to}&health={intolerance}&app_id={app_id}&app_key={app_key}").json()
        recipe_length = (len(url['hits']))


    for urls in range(recipe_length):
        recipe_name = (url['hits'][urls]['recipe']['label'])
        recipe_url = (url['hits'][urls]['recipe']['url'])
        recipe_image = (url['hits'][urls]['recipe']['image'])
        recipe_healthLabels = (url['hits'][urls]['recipe']['healthLabels'])
        recipe_ingredients = (url['hits'][urls]['recipe']['ingredientLines'])
        test.append((recipe_name, recipe_url, recipe_image, recipe_healthLabels, recipe_ingredients))

    context = {'test': test, 'form1': form1}

    return render(request, 'ingredior/ingredior.html', context)

ingredior.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Nake Recipes</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link rel="stylesheet" href=>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

    <style>
        .card {
        width: 300px;
        height: 700px;
        overflow: scroll;
        float: left;
        margin: 13px;
        }

        .list {
        line-height: 2em;
        text-align: left;
        }

        .card-content ul {
        height: 100px
        overflow-y: scroll;
        }

        .container h1 {
        text-align: center;
        }
    </style>

</head>
<body>
<nav>
    <div class="container nav-wrapper">
        <a href="/" class="brand-logo">Naked Recipes</a>
        <ul id="nav-mobile" class="right">
            <li><a href="/">Home</a></li>
        </ul>
    </div>
</nav>

<div class="container">
    <h1 style="text-align: c">What intolerance do you have?</h1>
    <div class="row">
        <div class="col s4">
            <!-- Promo Content 1 goes here -->
            <div class="center">
                <i class="large material-icons" style="color: #EE6E73">flash_on</i>
                <p></p>
                <p class="light center"></p>
            </div>
        </div>

        <div class="col s4">
            <!-- Promo Content 2 goes here -->
            <div class="center">
                <i class="large material-icons" style="color: orange">camera</i>
                <p></p>
                <p class="light center"></p>
            </div>
        </div>

        <div class="col s4">
            <!-- Promo Content 3 goes here -->
            <div class="center">
                <i class="large material-icons" style="color: blue">chrome_reader_mode</i>
                <p></p>
                <p class="light center"></p>
            </div>
        </div>
    </div>
    <br>
        <form action="" method="post">
            {% csrf_token %}
            {{ form1 }}
        </form>
    <br>
    <div class="row">
            {% for item in test %}
            <div class="col_s2">

                <div class="card">
                    <div class="card-image">
                        <img src= {{ item.2 }} alt="">
                    </div>
                        <div class="card-content scroll">
                            {{ item.0 }}<br><br>
                            <p>
                            <b>Ingredients</b><br>
                            </p>
                             <ul class="list">
                                {% for litem in item.4 %}
                                <li>- {{ litem }}</li>
                                 {% endfor %}
                             </ul>

                              <ul class="list">
                                <p>
                                    <b>Allergies</b>
                                </p>
                                {% for litem in item.3 %}
                                <li>- {{ litem }}</li>
                                {% endfor %}
                              </ul>
                        </div>
                    <div class="card-action">
                        <a href={{ item.1 }}>Get Full Recipe </a>
                    </div>
                </div>
            {% endfor %}
            </div>
    </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</body>
</html>

test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 <form action="" method="post" >
        {% csrf_token %}
        {{ form1 }}
    </form>
</body>
</html>

app / urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name="index"),
]

提前谢谢you

**更新**

进一步调查: Inspection Image ingredior.html Inspection Image test.html

它们是相同的。

0 个答案:

没有答案