简体   繁体   English

Django 中的 ModelForm 未显示

[英]ModelForm in Django not showing

I'm trying to display a basic form in django but does not render the form once I run the server, I would appreciate if you could help me with these, here my code.我正在尝试在 django 中显示一个基本表单,但是一旦我运行服务器就不会呈现表单,如果您能帮助我解决这些问题,我将不胜感激,这里是我的代码。

models.py模型.py

STATUS_OPTIONS =(
    ('OP', 'OPEN'),
    ('CL', 'CLOSED')
)

class Employee(models.Model):
    id = models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField(max_length=200)
    status = models.CharField(max_length=50, choices=STATUS_OPTIONS)
    password = models.CharField(max_length=100)

    def __str__(self):
        return self.first_name

forms.py forms.py

from django import forms
from .models import Employee

class EmployeeForm(forms.Form):
    class Meta:
        model = Employee
        fields = "__all__"

urls.py网址.py

from django.urls import include, path
from . import views

urlpatterns = [
    path('', views.create_employee),
]

views.py视图.py

from .forms import EmployeeForm

# Create your views here.

def create_employee(request):
    form = EmployeeForm()
    return render(request, 'employee/create_employee.html', {'form':form})

create_employee.html create_employee.html


<h1>Formu</h1>
<form action="" method='POST'>
{{form.as_p}}
<input type="submit">
</form>

When I run the program the only thing rendered is the tag, any idea?当我运行程序时,唯一呈现的是标签,知道吗?

In order to create forms out of models you need to use ModelForm not Form .为了从模型中创建 forms 您需要使用ModelForm而不是Form Otherwise you end up with a regular form without any field.否则,您最终会得到一个没有任何字段的常规表单。

from django.forms import ModelForm
from .models import Employee

class EmployeeForm(ModelForm):
    #...

More about ModelForm here更多关于ModelForm 在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM