【发布时间】: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