Django 不显示字段

Django doesn't show field

所以,我是 django 的新手,我正在学习 我创建了一个表单,但 Django 没有在页面中显示它 我正在使用 python 3.9.4 姜戈 3.2


这是我的代码

models.py

from django.db import models

# Create your models here.

class Commande(models.Model):
    name = models.CharField(max_length=300)

forms.py



from django import forms
from .models import Commande


class home_form(forms.ModelForm):
    class Meta:
        model = Commande
        fields = ['name']


class RawCmdForm(forms.Form):
    name = forms.CharField()

views.py

from .models import Commande
from django.shortcuts import render
from .forms import home_form, RawCmdForm

# Create your views here.
def home_view(request):
    medocs = Commande.objects.all()
    #print(medocs)
    context = {'medoc' : medocs}
    return render(request, "index.html", context)


def cmd_Form_view(request):
    my_form = RawCmdForm()
    if request.method == "POST":
        my_form = RawCmdForm(request.POST)
        if my_form.is_valid():
            print("Good Data")
            Commande.objects.create(**my_form.cleaned_data)
            my_form = RawCmdForm()

    #if request.method == "POST":
    #   new_name= request.POST.get("name")   
    #   Medoc.objects.create(name = new_name)
    context = {"form" : my_form}
    return render(request, "index.html", context)

index.html

<!DOCTYPE html>
<html>
<head>
    <title>My Commande</title>
</head>
<body>
    <h1 style="color: red"> Mes Manqants</h1>




{% for instance in medoc %}
    <input type="checkbox" id= {{instance.id}} name= {{instance.name}} value= {{instance.name}} >
    <label for= {{medoc.id}} > {{instance.name}} </label><br>

{% endfor %}
</body>


</html>

formadd.html

{% extends "index.html" %}



{% block content %}

{{ form }}

{% endblock %}

urls.py

from django.contrib import admin
from django.urls import path
from cmmd.views import home_view, cmd_Form_view
urlpatterns = [
    path('', home_view),
    path('formadd/', cmd_Form_view),
    path('admin/', admin.site.urls),
]

我试过遇到同样问题的人,我认为我做了我应该做的一切,但没有改变 那么问题出在哪里

index.html 文件中,您应该将 {% block content %}{% endblock %} 作为占位符,这样当它呈现 addform.html 并扩展 index.html 时,django 知道在哪里呈现表单。有关模板继承的更多信息,请访问:https://docs.djangoproject.com/en/3.2/ref/templates/language/#template-inheritance

<!DOCTYPE html>
<html>
<head>
    <title>My Commande</title>
</head>
<body>
    <h1 style="color: red"> Mes Manqants</h1>

    {% for instance in medoc %}
        <input type="checkbox" id= {{instance.id}} name= {{instance.name}} value= {{instance.name}} >
        <label for= {{medoc.id}} > {{instance.name}} </label><br>

    {% endfor %}

    <div id="content">
        {% block content %}{% endblock %}
    </div>
</body>


</html>

在您的 index.html 模板(正文内)中包括这些。 {% block content %} {% endblock %}

<body>
...

{% block content %}


{% endblock %}

</body>