2016-05-08 23 views
1

フォームを含むアプリケーションがあり、すべてのフィールドから返された結果が他のアプリケーションに送信されます。 Django NameError:名前 'フォーム'が定義されていません

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}) 

Choixの/ views.py私の電子メールフィールドの実行結果は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

それはNameErrorを返すのrunserver私はコマンドを設定
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}) 

:名 'フォームは' APSとメールで定義されていませんしかし、私はchoix/views.pyのように定義しました

答えて

7

forms.ModelFormを使用していますが、どこでformsをインポートしましたか?

あなたはこのようにそれをインポートする必要があります。ファイル「/home/pi/Desktop/jdid/sfe/aps/views.py:私はこのエラーがなくなっているが、今、私は別のエラーを持っている、それを変更した from django import forms

+0

"、12行目、Metaで model = Configuration NameError:name 'Configuration'が定義されていません – bne

+1

これはインポートされていないためです...' choix.models import configuration ' – Pythonista

+0

あなたの素早い回答ありがとうございますdjangoの初心者です。あなたを気にしていませんが、別のエラーが返されました:「更新が必要です。 %name django.core.exceptions.ImproperlyConfigured: 'fields'属性または 'exclude'属性のいずれかを使用せずにModelFormを作成することは禁止されています。フォームConfigurationFrormは更新が必要です。 – bne

関連する問題