edwards17
edwards17

Reputation: 83

No module named 'forms' Django

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

I receive an error saying No module named 'forms'. I am fairly 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

the forms.py is located in the same folder as views.py. I tried from django.forms import MyRegistrationForm but then the error cannot import name MyRegistrationForm arises.

Upvotes: 8

Views: 14761

Answers (2)

antimatter
antimatter

Reputation: 3480

If you didn't change the default location of views.py, then it's likely to be in your application folder. Try something like from myapp.forms import MyRegistrationForm where myapp is the name of your application

Upvotes: 16

httpdss
httpdss

Reputation: 111

if thats an app module, change your 6th line:

from forms import MyRegistrationForm

to:

from .forms import MyRegistrationForm

(just add a dot before forms)

Upvotes: 11

Related Questions