我的django项目中出现模块错误

时间:2018-03-08 06:10:28

标签: python django django-models

我正在构建一个新的django应用程序,出于某种原因,当我尝试从forms.py文件中的models.py文件访问模型时,我收到错误。

这是错误:

  File "/Users/omarjandali/Desktop/MySplit/mysplit/general/forms.py", line 13, in <module>
    from models import *
ModuleNotFoundError: No module named 'models'

然而,我已将常规应用添加到已安装的设置中。我保存了所有模型。为什么说模块不在那里......我要空白了。我知道这是一个简单的答案,但出于某种原因,我无法弄明白。

这是forms.py文件:

# all model imports related to this project
from models import *

class LoginForm(forms.Form):
    username = forms.CharField(max_length=20)
    password = forms.CharField(max_length=20, widget=forms.PasswordInput)

这里是models.py文件:

from django.db import models
from django.contrib.auth.models import User, UserManager
from localflavor.us.models import USStateField, USZipCodeField

# the following is the users profile model
class Profile(models.Model):
    # users basic info
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    f_name = models.CharField(max_length=25, default='first')
    l_name = models.CharField(max_length=25, default='last')
    bio = models.CharField(max_length=220, default='bio')
    # users location
    street = models.CharField(max_length=200, default='street address')
    city = models.CharField(max_length=100, default='city')
    state = USStateField(default='CA')
    zip_code = USZipCodeField(default=12345)
    # users other profile info
    phone = models.IntegerField(default="000-ooo-oooo")
    dob = models.DateField(default='1950-01-01')
    gender = models.CharField(max_length=5, default='Other')
    # lob = industry/occupation
    lob = models.CharField(max_length=40, default='occupation')
    # dba = Company Name
    dba = models.CharField(max_length=40, default='comapny')
    account_type = models.CharField(max_length=20, default='INDIVIDUAL')
    synapse_id = models.CharField(max_length=200, default='123456789')
    created = models.DateTimeField(auto_now_add=True)

2 个答案:

答案 0 :(得分:2)

from .models import *

你需要提供。用于同一目录的路径

你也可以给出绝对路径

from app_name.models import *

答案 1 :(得分:1)

from <app_name>.models import <class_name>

这将解决问题。在您的情况下,app_name是&#34; mysplit&#34;最有可能的

所以,你需要像这样导入:

from mysplit import *

&#34; *&#34;因为您要导入models.py文件中的所有内容 我希望这能解决您的问题