简体   繁体   English

我该如何解决这个错误? 我正在和 django 一起做一个学校项目,但我被困住了

[英]How can i solve this error? I'm working with django on a school project and i'm stuck

i get this error " ModuleNotFoundError: No module named 'models' ".我收到此错误“ModuleNotFoundError: No module named 'models'”。 I created a model called Job and I imported it to the admin.我创建了一个名为 Job 的模型并将其导入管理员。 But I have that error.但我有那个错误。 I've tried switching up the names of the model and some other folder on the project cause I read somewhere that could be the issue but I can't seem to get my head around it.我已经尝试在项目中切换模型和其他一些文件夹的名称,因为我在某个地方读到了可能是问题的地方,但我似乎无法理解它。

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone

class Job(models.Model):

    title = models.CharField(max_length=150)
    company = models.CharField(max_length=100)
    location = models.CharField(max_length=200)
    salary = models.CharField(max_length=100)
    nature = models.CharField(max_length=10, choices=TYPE_CHOICES, default=FULL_TIME)
    experience = models.CharField(max_length=10, choices=EXP_CHOICES, default=TIER1)
    summary = models.TextField()
    description = models.TextField()
    requirements = models.TextField()
    logo = models.ImageField (default='default.png', upload_to='upload_images')
    date_created = models.DateTimeField(default=timezone.now)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)


    def __str__(self):
        return '{} looking for {}'.format(self.company, self.title) 

then this is my input in the admin.py file那么这是我在 admin.py 文件中的输入

from django.contrib import admin
from models import Job

admin.site.register(Job)

You are getting this error because models is not a module.您收到此错误是因为模型不是模块。 Use this instead.改用这个。

from django.contrib import admin
from .models import Job

admin.site.register(Job)

What the '.'什么'。 does is find a models file in the directory the admin.py is in.确实是在 admin.py 所在的目录中找到一个模型文件。

Add a .添加一个. before models in your import in admin.py to specify that you want to look in the same directory, like so :admin.py中导入的模型之前指定要在同一目录中查找,如下所示:

from .models import Job

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM