No module named 'forms' Django

I am trying to initiate the registration process for my website. I am using Python 3.3.5 and Django 1.6.

I get the error No module named 'forms' . I am new to Python / Django.

Here are my files:

Views.py:

 from django.shortcuts import render_to_response from django.http import HttpResponseRedirect from django.contrib import auth from django.core.context_processors import csrf from django.contrib.auth.forms import UserCreationForm from forms import MyRegistrationForm def register_user(request): if request.method == 'POST': form = MyRegistrationForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/accounts/register_success') else: form = MyRegistrationForm() args = {} args.update(csrf(request)) args['form'] = form return render_to_response('register1.html', args) def register_success(request): return render_to_response('register_success.html') 

Forms.py

 from django import forms from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm class MyRegistrationForm(UserCreationForm): email = forms.EmailField(required=True) class Meta: model = User fields = ('username', 'email', 'password1', 'password2') def save(self, commit=True): user = super(MyRegistrationForm, self).save(commit=False) user.email = self.cleaned_data['email'] # user.set_password(self.cleaned_data['password1']) if commit: user.save() return user 

form.py is in the same folder as view.py. I tried to import MyRegistrationForm from django.forms, but then a cannot import name MyRegistrationForm error occurs.

+7
python django
source share
2 answers

If you did not change the locatoin default views.py , then this is most likely in your application folder. Try something like from myapp.forms import MyRegistrationForm , where myapp is the name of your application

+9
source share

if it is an application module, change your 6th line:

 from forms import MyRegistrationForm 

in

 from .forms import MyRegistrationForm 

(just add a dot in front of the forms)

+8
source share

All Articles