我正在做一个项目.我有一个模特:-

                class CustomersModels(models.Model):
                def servChoices(servs):
                    lst = [x.serv_name for x in servs]
                    ch =()
                    for a in lst:
                        ch += (a,a),
                    return ch

                customer_name = models.CharField(max_length=100)
                comp_name = models.CharField(max_length=100)
                ph_no = models.CharField(max_length=12)
                webs_name = models.URLField(max_length=200)
                service_insterested = models.OneToOneField(ServiceModel, on_delete = models.CASCADE)

                def __str__(self):
                    return self.customer_name

我有一个与这个型号对应的表格. 现在我想要的是字段customer_namecomp_namewebs_name 在一页中是可选的.并在另一页中要求. 请 bootstrap 我以最方便的方式确定任务

推荐答案

在‘def servChoices’中删除了SERVICE_INSTERESTED,因为我在模型中没有访问它们的权限.我还返回一个类名为‘CustomersModels’的字符串,这样您就可以编辑、删除包含空值的记录. 字段类‘CUSTOMER_NAME’、‘COMP_NAME’、‘WEBS_NAME’被设置为BLACK=True以使它们成为可选的.

在NoForm中,必填字段为‘ph_no’.YesForm要求填写所有字段.为此,使用了验证器(CLEAN),它将在页面上显示未填写的字段的消息(在填写完所有字段之前,不会发送表单).你可以读到关于清洁here:的文章

In the bboard views, replace with your folder where you have the templates (this is the string template_name = 'bboard/templ_yes.html').

Urls.py

urlpatterns = [
    path('yes/', YesCreateView.as_view(), name='yes'),
    path('no/', NoCreateView.as_view(), name='no'),
]

Views.py

from django.views.generic.edit import CreateView
from django.urls import reverse_lazy
from .forms import NoForm, YesForm

class NoCreateView(CreateView):
    template_name = 'bboard/templ_no.html'
    form_class = NoForm
    success_url = reverse_lazy('no')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        return context

class YesCreateView(CreateView):
    template_name = 'bboard/templ_yes.html'
    form_class = YesForm
    success_url = reverse_lazy('yes')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        return context

Forms.py

from django.forms import ModelForm
from .models import CustomersModels
from django.core.exceptions import ValidationError

class YesForm(ModelForm):
    class Meta:
        model = CustomersModels
        fields = ('customer_name', 'comp_name', 'ph_no', 'webs_name')

    def clean(self):
        cleaned_data = super().clean()
        customer_name = cleaned_data.get('customer_name')
        comp_name = cleaned_data.get('comp_name')
        webs_name = cleaned_data.get('webs_name')

        if len(customer_name) <= 0 or customer_name == '':
            raise ValidationError(
                "fill in the field customer_name"
            )
        if len(comp_name) <= 0 or comp_name == '':
            raise ValidationError(
                "fill in the field comp_name"
            )
        if len(webs_name) <= 0 or webs_name == '':
            raise ValidationError(
                "fill in the field webs_name"
            )
            
class NoForm(ModelForm):
    class Meta:
        model = CustomersModels
        fields = ('customer_name', 'comp_name', 'ph_no', 'webs_name')

Models.py

class CustomersModels(models.Model):
    customer_name = models.CharField(max_length=100, blank=True)
    comp_name = models.CharField(max_length=100, blank=True)
    ph_no = models.CharField(max_length=12)
    webs_name = models.URLField(max_length=200, blank=True)

    def __str__(self):
        return 'CustomersModels'

Tepmplate(templ_yes.html)

<h2>form</h2>
<form method="post" action="{% url 'yes'%}">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" value="adding">
</form>

Tepmplate(templ_no.html)

<h2>form</h2>
<form method="post" action="{% url 'no'%}">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" value="adding">
</form>

Django相关问答推荐

如何在创建对象后立即运行一次代码?

如何在Django模板中获取组中对象的整体计数器(&Q;)?

try 获取静态文件路径时 Django 给出错误

django 无法识别实现自定义后端

如何在 Fargate 容器中运行的 Django 中使用 AWS SES?

UpdateView 不会对 from 属性进行数据绑定

Django:获取每组最新的N条记录

如何重命名标准 django-registration 模板的名称

Django 模型 Select - 只允许管理界面上的特定转换

Django celery 页面给出 404

使用 Django 一次更新多条记录

django 用一个提交按钮提交两种不同的表单

virtualenv(python3.4), pip install mysqlclient 错误

如何验证对象是否存在于多线程中

使用 sqlite 运行 django 测试

Python Django:您正在使用 staticfiles 应用程序而没有设置 STATIC_ROOT 设置

Celery - 在一台服务器上运行不同的工作人员

如何仅在某些情况下禁用 Django 的 csrf 保护?

Django:如何将数据保存到 ManyToManyField?

Django:在还原(迁移)后try 访问数据库时权限被拒绝