我有一个包含表单的应用程序,每个字段返回的结果都将发送给其他应用程序。 choix/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})
我的电子邮件字段结果将发送到我的邮件应用程序视图 邮件/视图.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.')
我的temps将被发送到我的aps应用程序 aps/视图.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'未在aps和mail中定义 但是我定义它就像在choix/views.py中那样
您使用的是
forms.ModelForm
,但是您在哪里导入过forms
?您应该这样导入它:
from django import forms
相关问题 更多 >
编程相关推荐