django - ModelForm 在使用模型表单时没有指定模型类
问题描述
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 返回 _bootstrap._gcd_import(name[level:], package, level) 文件 "",第 1030 行,在 _gcd_import 文件“”中,第 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 行,来自 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)
解决方案
首先,您应该发布一些代码,例如您的 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__
推荐阅读
- ios - 如何正确设置高度约束
- sql - 在 SQL 中计算 XIRR
- python - 端口 443 上的 websocket 与 kubernetes 下的 /socket.io 不工作
- html - 如何在 Bootstrap 4.1.3 中的水平单选按钮下方显示验证消息?
- regex - 如何使用正则表达式查找特定单词并返回以下 10 个字符?
- loops - 试图找到一种使用 johnny-five.io 以不同速度控制伺服系统的好方法
- javascript - 仅在已加载图像后执行图像分析
- c# - RestSharp - 如何在不上传文件的情况下发送多部分/表单数据正文请求?
- java - 产品 ini 文件名未设置为启动器名称
- google-apps-script - 在哪里可以找到(跟踪)Google Apps Script API 更改和新功能?