我正在尝试为我的网站启动注册过程.我使用的是Python 3.3.5和Django 1.6.
我收到一个错误说No module named 'forms'
.我是Python/Django的新手.
这是我的文件:
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
forms.py与views.py位于同一文件夹中.我尝试从django.forms导入MyRegistrationForm,但随后出现错误cannot import name MyRegistrationForm
.
如果您没有更改默认的locatoin views.py
,那么它可能在您的应用程序文件夹中.尝试像from myapp.forms import MyRegistrationForm
这里myapp
是你的应用程序的名称
如果这是一个应用程序模块,请更改您的第6行:
from forms import MyRegistrationForm
至:
from .forms import MyRegistrationForm
(只需在表格前添加一个点)