我有一个名为choix的应用程序,其中包含电子邮件字段(将作为收件人的电子邮件发送到邮件应用程序)包含时间值的整数字段(将被发送到另一个应用程序aps到其模板作为显示图表的频率)我的目标是通过我在choix视图中创建的数据库发送数据,但它返回了一个错误:NameError:全局名称'form'未定义。我猜我的语法根本不正确 喜爱将/ views.py
from django.shortcuts import render
from choix.forms import ConfigurationForm
from django.http import HttpResponse, HttpResponseRedirect
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}), 'index.html', form=form, f=temps)
return render(request, 'configuration.html', {'form': form})
邮件/ views.py
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from choix import views
def index(request):
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')
post = m.Post.objects.create(e_mail=e_mail)
post.save()
to = form.e_mail.data
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:
return HttpResponse('Make sure all fields are entered and valid.')
APS / views.html
from django.shortcuts import render
from rasp import foo
from choix import views
import json
def index(request):
return render(request, 'index.html', {'t' : foo(), 'form':form, 'f':temps})