[英]ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package
Currently working on a Django website and I'm having an issue importing a class and its attributes from my models.py
module into my views.py
module within my music app. 目前正在Django网站上工作,我在将一个类及其属性从models.py
模块导入我的音乐应用程序中的views.py
模块时遇到问题。 From what I understand Django uses metaclasses to construct models, so the defined fields don't end up as attributes on the class. 根据我的理解,Django使用元类来构造模型,因此定义的字段不会最终作为类的属性。 Is this the problem and if so how do I solve it? 这是问题,如果是这样,我该如何解决?
Directory Structure: 目录结构:
music
migrations
templates
_init_
admin.py
apps.py
models.py
tests.py
urls.py
views.py
The code inside models.py
is: models.py
的代码是:
from django.db import models
class Album(models.Model):
artist = models.CharField(max_length=250)
album_title = models.CharField(max_length=500)
genre = models.CharField(max_length=100)
album_logo = models.CharField(max_length=1000)
def __str__(self):
return self.album_title + ' - ' + self.artist
The code inside views.py
is: views.py
的代码是:
from django.http import HttpResponse
from .models import Album
from django.template import loader
def index(request):
all_albums = Album.object.all()
template = loader.get_template('music/index.html')
context = {'all albums': all_albums,}
return HttpResponse(template.render(context, request))
def details(request, album_id):
return HttpResponse("<h2> Details for Album id: " + str(album_id) + " </h2>")
The Error: 错误:
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package
Error Location: from .models import Album
错误位置: from .models import Album
您的__str__
方法必须定义到Album
类中,否则self是未定义的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.