Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError: No module named 'forms'

Tags:

python

django

New to django, I am trying to understand this error. The forms is in the folder of the project. My forms.py is importing from django forms.

class LoginForm(forms.Form): 
email = forms.EmailField(label='Courriel')
password = forms.CharField(label='Mot de passe',
                        widget = forms.PasswordInput)

My views.py is importing LoginForm

I don't know why I am getting this error?

Thanks

like image 523
Arielis Avatar asked Feb 24 '17 17:02

Arielis


2 Answers

You're probably importing LoginForm in views.py as following:

from forms import LoginForm

Change that to

from .forms import LoginForm
like image 96
narendra-choudhary Avatar answered Nov 20 '22 00:11

narendra-choudhary


It sounds like it should be working, you might want to post the full error and more of your code. Something like this should be working

# forms.py
from django import forms


class LoginForm(forms.Form): 
    email = forms.EmailField(label='Courriel')
    password = forms.CharField(label='Mot de passe',
                        widget = forms.PasswordInput)

and the views

# views.py
from forms import LoginForm
from django.shortcuts import render


def someview(request):
    if request.method == 'POST':
        login_form = LoginForm(data=request.POST)
        if login_form.is_valid():
            # do something when valid
    else:
        login_form = LoginForm()
    return render(request, 'login.html', {'login_form': login_form})

where you have a folder structure like

/app/
   forms.py
   views.py
like image 34
davidejones Avatar answered Nov 20 '22 01:11

davidejones