gpt4 book ai didi

python - Django 名称错误 : name 'forms' is not defined

转载 作者:行者123 更新时间:2023-11-28 19:48:03 25 4
gpt4 key购买 nike

我有一个应用程序,其中包含一个表单,每个字段返回的结果将被发送到其他应用程序。选择/views.py

from django.shortcuts import render
from choix.forms import ConfigurationForm
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from choix.models import Configuration
from django import forms
class Meta:
model = Configuration
def index(request):
if request.method == 'GET' :
form = ConfigurationForm()
else:
form = ConfigurationForm(request.POST)
if form.is_valid():
e_mail = form.e_mail.data['e_mail']
temps = form.temps.data['temps']
temperature = form.temperature.data['temperature']
post = m.Post.objects.create(e_mail=e_mail,
temps=temps, temperature = temperature)
post.save()
return HttpResponseRedirect(reverse('post_detail', kwargs={'post_id' : post.id}))


return render(request, 'choix/configuration.html', {'form': form})

我的电子邮件字段结果发送到我的邮件应用 View 邮件/views.py

from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from choix import views
from choix.forms import ConfigurationForm
from django import forms
class ConfigurationFrorm(forms.ModelForm):
class Meta:
model = Configuration
def index(request,self):

subject = request.POST.get('subject', 'subject')
message = request.POST.get('message', 'attention ! la temperature a depasse le maximum ')
from_email = request.POST.get('from_email', '*********@gmail.com')
cleaned_data = super(ConfigurationForm, self).clean()
to = cleaned_data.get("email")
if subject and message and from_email:
try:
send_mail(subject, message, from_email, [ to ])
return HttpResponse('templates/mail.html')
except BadHeaderError:
return HttpResponse('Invalid header found.')
return HttpResponseRedirect('mail')
else:
# In reality we'd use a form class
# to get proper validation errors.
return HttpResponse('Make sure all fields are entered and valid.')

我的临时工将被发送到我的 aps 应用程序aps/views.py

from django.shortcuts import render
from rasp import foo
from choix import views
from choix.forms import ConfigurationForm
from django import forms
import json


class ConfigurationFrorm(forms.ModelForm):
class Meta:
model = Configuration
def index(request,self):
cleaned_data = super(ConfigurationForm, self).clean()
temps = cleaned_data.get("temps")

return render(request, 'index.html', {'t' : foo(), 'form':form, 'f':temps})

当我设置命令 runserver 它返回 NameError: name 'forms' is not defined in aps and mail 但是我已经像在 choix/views.py 中那样定义了它

最佳答案

您正在使用 forms.ModelForm,但是您在哪里导入 forms

你应该像这样导入它:从 django 导入表单

关于python - Django 名称错误 : name 'forms' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37102452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com