我有一个带自定义登录表单的自定义登录页面。此代码适用于Django 1.6.6。但是当我升级到Django 1.8时,我开始在/获取ImportError 没有名为form的模块。如果我删除了我的自定义authentication_form和password_reset_form,那么"工作"。
from django.conf.urls import include
from django.conf.urls import patterns
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from users.forms import PlayingAuthenticationForm, PlayingPasswordResetForm
from django.conf import settings
urlpatterns = patterns('',
url(r'^logga-in/$',
"users.views.login",
{'extra_context': {'facebook_redirect_uri': settings.FACEBOOK_REDIRECT_URI, 'google_redirect_uri': settings.GOOGLE_REDIRECT_URI, }, 'template_name': 'registration/login.html', 'authentication_form': PlayingAuthenticationForm},
name='auth_login'),
url(r'^logga-ut/$',
auth_views.logout,
{'template_name': 'registration/logout.html'},
name='auth_logout'),
url(r'^byt-losenord/$',
auth_views.password_change,
name='password_change'),
url(r'^byt-losenord/klar/$',
auth_views.password_change_done,
name='password_change_done'),
url(r'^glomt-losenord/$',
auth_views.password_reset,
{'password_reset_form': PlayingPasswordResetForm },
name='password_reset'),
url(r'^password/reset/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
auth_views.password_reset_confirm,
name='password_reset_confirm'),
url(r'^password/reset/complete/$',
auth_views.password_reset_complete,
name='password_reset_complete'),
url(r'^password/reset/done/$',
auth_views.password_reset_done,
name='password_reset_done'),
)
应该有用,对吗?我无法找到导入表单的任何问题,而且forms.py仍在用户应用中。
编辑:完成追溯:
Environment:
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.8
Python Version: 2.7.8
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.flatpages',
'django.contrib.humanize',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
'django.contrib.sites',
'adminforum',
'common',
'social',
'forums',
'localflavor',
'mailsender',
'menu',
'notifications',
'pm',
'users',
'registration')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'users.middleware.UserMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
119. resolver_match = resolver.resolve(request.path_info)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
366. for pattern in self.url_patterns:
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in url_patterns
402. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
396. self._urlconf_module = import_module(self.urlconf_name)
File "C:\Python27\lib\importlib\__init__.py" in import_module
37. __import__(name)
File "C:\playing\playing_se\urls.py" in <module>
24. url(r'^medlem/', include('registration.backends.default.urls')),
File "C:\Python27\lib\site-packages\django\conf\urls\__init__.py" in include
33. urlconf_module = import_module(urlconf_module)
File "C:\Python27\lib\importlib\__init__.py" in import_module
37. __import__(name)
File "C:\playing\apps\registration\backends\default\urls.py" in <module>
78. (r'', include('registration.auth_urls')),
File "C:\Python27\lib\site-packages\django\conf\urls\__init__.py" in include
33. urlconf_module = import_module(urlconf_module)
File "C:\Python27\lib\importlib\__init__.py" in import_module
37. __import__(name)
File "C:\playing\apps\registration\auth_urls.py" in <module>
31. from users.forms import PlayingAuthenticationForm, PlayingPasswordResetForm
Exception Type: ImportError at /
Exception Value: No module named forms
我的forms.py:
from django.forms.models import ModelForm
from django import forms
from django.utils.safestring import mark_safe
from users.models import GENDER_CHOICES, UserProfile
from django.forms.extras.widgets import SelectDateWidget
from django.forms.widgets import FileInput
from localflavor.se.forms import SEPostalCodeField
from django.contrib.auth.forms import AuthenticationForm, PasswordResetForm
from django.utils.translation import ugettext as _
from django.contrib.auth import get_user_model
from flanker.addresslib import address
class HorizRadioRenderer(forms.RadioSelect.renderer):
""" this overrides widget method to put radio buttons horizontally
instead of vertically.
"""
def render(self):
"""Outputs radios"""
return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))
class UserProfileForm(ModelForm):
gender = forms.CharField(widget=forms.RadioSelect(renderer=HorizRadioRenderer,
choices=GENDER_CHOICES), required=False, label="Kön")
birthday = forms.DateField(widget=SelectDateWidget(years=range(1900, 2012)), required=False, label="Födelsedatum")
description = forms.CharField(label="Presentation", required=False, widget=forms.Textarea(attrs={'maxlength': 530}))
zipcode = SEPostalCodeField(label="Postnummer", required=False)
class Meta:
model = UserProfile
fields = ['description', 'gender', 'birthday', 'first_name', 'last_name', 'address', 'zipcode', 'city' ]
class UserAvatarForm(ModelForm):
avatar_image = forms.ImageField(widget=FileInput, required=True)
class Meta:
model = UserProfile
fields = ['avatar_image',]
exclude = ['avatar']
class PlayingAuthenticationForm(AuthenticationForm):
username = forms.CharField(label=_("Username"), max_length=30, widget=forms.TextInput(attrs={'placeholder': 'Användarnamn', 'autofocus':'autofocus'}))
password = forms.CharField(label=_("Password"), widget=forms.PasswordInput(attrs={'placeholder': 'Lösenord'}))
remember_me = forms.BooleanField(initial=True, required=False)
class PlayingPasswordResetForm(PasswordResetForm):
def clean_email(self):
if not get_user_model().objects.filter(email__iexact=self.cleaned_data['email']):
raise forms.ValidationError("Det finns inget användarkonto kopplat till den e-postadressen.")
return self.cleaned_data['email']
class UserEmailForm(forms.Form):
email = forms.EmailField()
def clean_email(self):
data = self.cleaned_data['email']
if get_user_model().objects.filter(email__iexact=data):
raise forms.ValidationError("E-postadressen är redan upptagen. Försök med en annan.")
valid_email = address.validate_address(data)
if not valid_email:
raise forms.ValidationError("E-postadressen felaktig. Försök med en annan.")
return data
答案 0 :(得分:0)
让它发挥作用。我在auth_urls.py中删除了'authentication_form':PlayingAuthenticationForm,并在我的自定义视图中添加了authentication_form = PlayingAuthenticationForm。