Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django NameError: name 'forms' is not defined

Tags:

python

django

I have an application that contains a form the result returned by every field is going to be sent to others application . 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})

my e-mail field result is sent to my mail application views mail/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.')

my temps is going to be sent to my aps application 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})

when I set the command runserver it returns NameError: name 'forms' is not defined in aps and mail But I have defined it like I did in choix/views.py

like image 410
bne Avatar asked Dec 03 '22 15:12

bne


1 Answers

You're using forms.ModelForm, but where do you ever imports forms?

You should import it like this: from django import forms

like image 161
Pythonista Avatar answered Dec 16 '22 19:12

Pythonista