【问题标题】:ModelForm has no model class specified while using model formModelForm 在使用模型表单时没有指定模型类
【发布时间】:2020-11-14 05:28:48
【问题描述】:

path('', include('farmingwave.urls')),文件“F:\farm\lib\site-packages\django\urls\conf.py”,第 34 行,包含 urlconf_module = import_module(urlconf_module) 文件 "C:\Users\Mohit\AppData\Local\Programs\Python\Python39\lib\importlib_init_.py", 第 127 行,在 import_module 中 return _bootstrap._gcd_import(name[level:], package, level) File "", line 1030, in _gcd_import File "",第 1007 行,在 _find_and_load 文件中 "",第 986 行,在 _find_and_load_unlocked 文件“”,第 680 行,在 _load_unlocked
文件“”,第 790 行,在 exec_module 文件“”,第 228 行,在 call_with_frames_removed 文件“F:\farm\fwave\farmingwave\urls.py”,第 3 行,在 从 。导入视图文件“F:\farm\fwave\farmingwave\views.py”,第 3 行,在 从 .forms 导入 ContactForm 文件“F:\farm\fwave\farmingwave\forms.py”,第 3 行,在 from django.forms import contactForm ImportError: cannot import name 'contactForm' from 'django.forms' (F:\farm\lib\site-packages\django\forms_init.py)

我的意见.py

from django.shortcuts import render
from django.http import HttpResponse 
from .forms import contactForm
from django.forms import ModelForm
from django.core.mail import send_mail

def contact(request):
    if request.method == 'GET':
        form = contactForm()
    else:
        form = contactForm(request.POST)
        if form.is_valid():
            form.save()
            name = form.cleaned_data['name']
            email = form.cleaned_data['email']
            Phonenumber = form.cleaned_data['Phonenumber']
            try:
                send_mail(subject, message, from_email,['admin@example.com'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('success')
    return render(request, "contact-form.html", {'form': form})

我的表单.py

from django import forms
from django.core.validators import RegexValidator
# from django.forms import contactForm
from .models import contactForm



class contactForm(forms.ModelForm):

    name = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'class' : 
    'form-control col-lg-12', 'placeholder': 'Name'}), label='')
    email = forms.EmailField(widget=forms.TextInput(attrs={'class' : 'form-control col-lg-12', 'placeholder': 'Email'}), label='')
    Phonenumber = forms.CharField(max_length=10, validators= [RegexValidator(r'^\d{1,10}$')], widget=forms.TextInput(attrs={'class' : 'form-control col-lg-12', 'placeholder': 'Mobile'}), label='')

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

我的模型.py

from django.db import models
from django.forms import ModelForm
from django.core.validators import RegexValidator

class contactForm(models.Model):
    name = models.CharField(max_length=255)
    Phonenumber = models.CharField(max_length=10, validators=[RegexValidator(r'^\d{1,10}$')])
    email = models.EmailField(max_length=254)

【问题讨论】:

    标签: django twitter-bootstrap django-models django-forms


    【解决方案1】:

    首先,您应该发布一些类似 forms.py 的代码以进行澄清,但看到错误消息,它显示 django.forms import contactForm cannot import name 'contactForm' from 'django.forms'。似乎 contactForm 是名称您要在其上塑造表单的模型。 作为模型,我们如何从 django.forms 导入它? 使用此代码;

    from .models import contactForm
    from django.forms import modelForm #modelForm helps to create forms from models
    
    
    class yourForm(modelForm):
        class Meta:
            model = contactForm #here we provide the name of the model from which the form is to be inherited
            fields = ('here_provide_the_name_of_the_fields_of_the_model_which_you_want_to_Include_in_the_form')
    

    不同的字段将被放置为字段元组的不同元素。 如果您想在表单中包含所有字段;

    fields = __all__
    

    【讨论】:

    • 我确实按照你说的发布了我的 form.py
    • 首先你不需要在你的models.py中导入modelForm。虽然这不是什么大问题。第二个问题是您正在预定义的contactForm 类中创建另一个类contactForm,这没有任何意义。您想根据您的模型创建字段,但首先您在 contactForm 中手动创建它们,然后您再次在已创建的类中定义另一个具有相同名称的类,并尝试根据您的模型获取字段。你真正需要的是用你在contactForm类中定义的类替换整个类。
    • 简而言之,只需按照我的回答即可。
    • 我的答案是为您的项目完成 forms.py。
    • 再次我想提一下,contactForm 是您创建的模型,不属于 django.forms。所以从 django.forms 导入它没有任何意义。
    猜你喜欢
    • 1970-01-01
    • 2021-02-23
    • 2014-05-18
    • 2012-02-19
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    相关资源
    最近更新 更多