【问题标题】:cannot import form in django无法在 django 中导入表单
【发布时间】:2012-08-03 01:47:31
【问题描述】:

我遇到了烦人的问题.. 我试图使用自定义字段来扩展用户注册... 如下

导入哈希库 导入日期时间 导入 hmac 从 django.db 导入模型

from django.contrib.auth.models import User
from ecpCommon.models import StateModel
from ecpCommon.enum import enumauto
from ecpPayments.models import PaymentCard
from registration.signals import user_registered
from apps.ecpUser.models import UserProfile
from apps.ecpMerchant.forms import RegistrationFormEx


   class MerchantProfile(StateModel):
      user = models.OneToOneField(UserProfile, related_name="merchant_profile")
      payment_card = models.OneToOneField(PaymentCard, related_name="merchant_profile")
      current_state = models.IntegerField('State', choices=STATE_CHOICES)
      name = models.CharField('Merchant Name', max_length=64)

      def user_created(sender, user, request, **kwargs):
            form = RegistrationFormEx(data=request.POST)
            new_user = User.objects.get(username=request.POST['username'])
            digest=hmac.new("qnscAdgRlkIhAUPY44oiexBKtQbGY0orf7OV1I50", str(request.POST['password1']),hashlib.sha1).hexdigest()
            new_profile = UserProfile(user=new_user,api_key=digest)
            new_profile.save()
            #now add other fields including password hash as well
            uid = new_profile.id

            merchant_profile = MerchantProfile(user_id=uid,
                create_time=datetime.datetime.now(),
                modified_time=datetime.datetime.now(),
                payment_card_id=uid,
                current_state=1,
                name=request.POST['name'],
                 )
            merchant_profile.save()

我的表格如下

from django import forms
from registration.forms import RegistrationForm
from django.utils.translation import ugettext_lazy as _
from registration.models import RegistrationProfile
from apps.ecpMerchant.models import Category

class RegistrationFormEx(RegistrationForm):
    #ADD ALL CUSTOM FIELDS BELOW
    name=forms.CharField()

但它显示以下错误日志.. 早些时候它正在工作.. 后来错误来了.. 我在其他表的模型中进行了一些修改......但是它如何影响这个注册

C:\Python27\Lib\site-packages\django\bin\ecp>manage.py runserver
Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of <dja
ngo.contrib.staticfiles.management.commands.runserver.Command object at 0x02DC0A
B0>>
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.
py", line 91, in inner_run
    self.validate(display_num_errors=True)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 266,
 in validate
    num_errors = get_validation_errors(s, app)
  File "C:\Python27\lib\site-packages\django\core\management\validation.py", lin
e 30, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 158, in
 get_app_errors
    self._populate()
  File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 67, in
_populate
    self.load_app(app_name)
  File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 88, in
load_app
    models = import_module('.models', app_name)
  File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in im
port_module
    __import__(name)
  File "C:\Python27\Lib\site-packages\django\bin\ecp\apps\ecpMerchant\models.py"
, line 10, in <module>
    from apps.ecpMerchant.forms import RegistrationFormEx
  File "C:\Python27\Lib\site-packages\django\bin\ecp\apps\ecpMerchant\forms.py",
 line 5, in <module>
    from apps.ecpMerchant.models import Category
  File "C:\Python27\Lib\site-packages\django\bin\ecp\apps\ecpMerchant\models.py"
, line 10, in <module>
    from apps.ecpMerchant.forms import RegistrationFormEx
ImportError: cannot import name RegistrationFormEx

【问题讨论】:

  • 我认为这是您必须坐下来调试的时候之一。尝试像这样使用 python 的 pdb 'import pdb; set_trace()' 并在终端中,尝试在视图函数的各个部分(从模型到视图的开头等)导入表单。希望这可以帮助您找到错误。
  • 循环导入?表单导入模型和模型导入表单?
  • @john mee : 是的,你能把它作为答案吗.. 这样我就可以接受了

标签: python django import


【解决方案1】:

循环导入...

您有导入表单的表单导入模型。

【讨论】:

  • @Clayton 重构它,使模型不依赖于表单。对于给定的示例,在模型的 'user_created' 方法中完成的工作应移动到注册表单的方法中。
  • 我真的很喜欢这个主意。谢谢。
猜你喜欢
  • 2021-08-15
  • 2017-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
  • 2018-06-26
  • 2021-07-06
相关资源
最近更新 更多