Django表单没有显示所有字段

vom3gejh  于 2023-03-13  发布在  Go
关注(0)|答案(3)|浏览(182)

我有一个问题,我需要创建像方案的形式,我有我的模型和创建的形式。py。当我添加我的形式到HTML中,我只有2的所有字段在我的形式。它可以是什么?这是我的models.py

class Post(models.Model):
    full_name = models.CharField(max_length=60, verbose_name='Full name'),
    name_scheme = models.CharField(max_length=40, verbose_name='Name of scheme')
    job = models.CharField(max_length=100, verbose_name='Job'),
    email = models.EmailField(verbose_name='Email'),
    domain_name = models.CharField(max_length=100, verbose_name='Domain'),
    phone_number = PhoneNumberField,
    company_name = models.CharField(max_length=30, verbose_name='Company name'),
    text = models.TextField(max_length=255, verbose_name='Text'),
    age = models.IntegerField(validators=[MinValueValidator(18), MaxValueValidator(65)])
    address = models.CharField(max_length=255, verbose_name='Address'),
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.full_name

forms.py

class AddSchemeForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = '__all__'

views.py

def add_scheme(request):
    if request.method == 'POST':
        form = AddSchemeForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('home')
    else:
        form = AddSchemeForm()
        return render(request, 'add_scheme.html', {'form': form})

模板:

<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <div>
      {{ form.full_name.label_tag }}
      {{ form.full_name }}
    </div>
    <div>
      {{ form.name_scheme.label_tag }}
      {{ form.name_scheme }}
    </div>
    <div>
      {{ form.job.label_tag }}
      {{ form.job }}
    </div>
w8rqjzmb

w8rqjzmb1#

问题出在语法上。问题出在您使用的每个字段定义后面的逗号(,)上。逗号用于分隔列表中的项目,因此在本例中,它们会导致每个字段被视为单个项目元组,这不是您想要的。
下面是您可以使用的模型类代码:

class Post(models.Model):
    full_name = models.CharField(max_length=60, verbose_name='Full name')
    name_scheme = models.CharField(max_length=40, verbose_name='Name of scheme')
    job = models.CharField(max_length=100, verbose_name='Job')
    email = models.EmailField(verbose_name='Email')
    domain_name = models.CharField(max_length=100, verbose_name='Domain')
    phone_number = PhoneNumberField
    company_name = models.CharField(max_length=30, verbose_name='Company name')
    text = models.TextField(max_length=255, verbose_name='Text')
    age = models.IntegerField(validators=[MinValueValidator(18), MaxValueValidator(65)])
    address = models.CharField(max_length=255, verbose_name='Address')
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.full_name
xtupzzrd

xtupzzrd2#

你的模型字段最后似乎有一个,,因为它们是独立的字段,所以应该是这样的Python语句:

class Post(models.Model):
    full_name = models.CharField(max_length=60, verbose_name='Full name')
    name_scheme = models.CharField(max_length=40, verbose_name='Name of scheme')
    job = models.CharField(max_length=100, verbose_name='Job')
    email = models.EmailField(verbose_name='Email')
    domain_name = models.CharField(max_length=100, verbose_name='Domain'),
    phone_number = PhoneNumberField()
    company_name = models.CharField(max_length=30, verbose_name='Company name')
    text = models.TextField(max_length=255, verbose_name='Text')
    age = models.IntegerField(validators=[MinValueValidator(18), MaxValueValidator(65)])
    address = models.CharField(max_length=255, verbose_name='Address')
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.full_name

我还修复了你的PhoneNumberField。它没有正确初始化。
您可以在模板中使用它,如下所示:

{% extends 'base.html' %}

{% block content %}
    <h2>Add Scheme</h2>
    <form method="POST" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
{% endblock %}
jm81lzqq

jm81lzqq3#

如果要显示所有字段,可以简单地使用{{form.as_p}}。目前,您手动显示两个字段。
同时删除模型中多余的逗号,以便:

class Post(models.Model):
    full_name = models.CharField(max_length=60, verbose_name='Full name')
    name_scheme = models.CharField(max_length=40, verbose_name='Name of scheme')
    job = models.CharField(max_length=100, verbose_name='Job')
    email = models.EmailField(verbose_name='Email')
    domain_name = models.CharField(max_length=100, verbose_name='Domain')
    phone_number = PhoneNumberField()
    company_name = models.CharField(max_length=30, verbose_name='Company name')
    text = models.TextField(max_length=255, verbose_name='Text')
    age = models.IntegerField(validators=[MinValueValidator(18), MaxValueValidator(65)])
    address = models.CharField(max_length=255, verbose_name='Address')
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.full_name

相关问题