【问题标题】:Django doesn't show fieldDjango不显示字段
【发布时间】:2021-07-19 02:32:55
【问题描述】:

所以,我是 django 的新手,我正在学习 我创建了一个表单,但 django 没有在页面中显示它 我正在使用 python 3.9.4 django 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),
]

我尝试与遇到相同问题的人一起尝试,我认为我做了我应该做的一切,但没有改变想法 那么问题出在哪里

【问题讨论】:

  • 1) 你的cmd_Form_view 渲染index.html,而我想你想渲染formadd.html。 2)formadd.html扩展index.html并尝试填充一个不存在的块content{% block content %})...
  • 非常感谢您的帮助,它有效
  • 如果要在index.html中渲染表单怎么办??

标签: django forms field


【解决方案1】:

在您的 index.html 模板(主体内部)中包含这些。 {% 块内容 %} {% endblock %}

<body>
...

{% block content %}


{% endblock %}

</body>

【讨论】:

    【解决方案2】:

    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>
    

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 2014-04-05
      • 2015-08-20
      • 2018-06-25
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多