我在导入特定模块中的表单字段时遇到问题 在我的forms.py中,我有以下代码
from django import forms
from django.forms import ModelForm
from koalified.seeker.models import *
from django.contrib.auth.models import User
class RegistrationForm(ModelForm):
username = forms.CharField(label=(u'Username'))
password = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))
ver_password = forms.CharField(label=(u' Verify Password'), widget=forms.PasswordInput(render_value=False))
email = forms.EmailField()
class Meta:
model = Seeker
exclude = ('user','verified','premium')
def clean_username(self):
''' Checking username availability '''
username = self.cleaned_data['username']
try:
User.objects.get(username=username)
except User.DoesNotExist:
if len(username) >= 5 and len(username) < 21:
return self.cleaned_data['username']
else:
raise forms.ValidationError("Username must be between 5 and 20 characters")
raise forms.ValidationError("That username is already in use")
def clean_ver_password(self):
password = self.cleaned_data.get('password')
ver_password = self.cleaned_data.get('ver_password')
if not ver_password:
raise forms.ValidationError("You must confirm your password")
if password != ver_password:
raise forms.ValidationError("Passwords do not match")
return ver_password
我一直看到一个不存在的错误错误,我已经研究过,它建议进入shell并尝试导入。以下是导入
的以下输出Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/jmcguffee1/django.koalified.com/koalified/koalified/seeker/forms.py", line 6, in <module>
class RegistrationForm(ModelForm):
File "/home/jmcguffee1/django.koalified.com/koalified/koalified/seeker/forms.py", line 7, in RegistrationForm
username = forms.CharField(label=(u'Username'))
AttributeError: 'module' object has no attribute 'CharField'
我已经查看了一些问题并发现人们可能有拼写问题,但我一遍又一遍地试过这个并且无法在我的其他模块中加载表单工作就好了,比如我的登录形式
from django.contrib.auth.models import User
from django import forms
class LoginForm(forms.Form):
username = forms.CharField(label=(u'Username'))
password = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))
我已尝试手动导入字段,但它可以正常工作,即
from django.forms.fields import CharField, EmailField
username = CharField()
这很好用 我已经回到我的shell并导入了django.forms并尝试从那里加载
来自django导入表单 username = forms.CharField(label =(u'Username')) 用户名
我已经在models.py和views.py中检查过循环调用,并且django表单没有其他导入。看来这个特定模块没有正确加载表单字段。如果不够清楚,可以提供更多信息。
以下是答案的解决方案
>>> from django import forms
>>> print 'form is : ', forms
form is : <module 'django.forms' from '/home/jmcguffee1/django.koalified.com/kf/lib/python2.6/site-packages/Django-1.4.3-py2.6.egg/django/forms/__init__.pyc'>
>>> from koalified.seeker.models import *
>>> print 'form is : ', forms
form is : <module 'django.forms.forms' from '/home/jmcguffee1/django.koalified.com/kf/lib/python2.6/site-packages/Django-1.4.3-py2.6.egg/django/forms/forms.pyc'>
>>>
答案 0 :(得分:0)
这是猜测:在models.py文件中,您是否导入表单模块?我猜你做到了。那么这一行就是问题所在:
from koalified.seeker.models import *
将models模块中的每个全局名称导入当前名称空间。由于其中一个名称是forms
,它引用了当前的表单模块,因此它会覆盖您在顶部的导入:因此forms
现在引用相同的模块,而不是Django表单模块。
这就是**你永远不应该使用from x import *
的原因。要么只是from koalified.seeker import models
并参考models.Seeker
等,要么from koalified.seeker.models import Seeker, TheOtherModel
等等.IMO第一个更好,但这是一个偏好问题。
答案 1 :(得分:0)
明星导入是制作命名空间混乱的可靠方法。尝试添加一些打印语句,如下所示,并检查星形导入后“形式”名称的绑定
from django import forms
print "forms is :", forms
# ....
from koalified.seeker.models import *
print "forms is :", forms