简体   繁体   English

ModuleNotFoundError:没有名为'__main __。models'的模块; '__main__'不是包

[英]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.

相关问题 我遇到一个错误“ ModuleNotFoundError:没有名为&#39;__main __。models&#39;的模块; “ __main__”不是软件包” - I'm having an error “ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package” ModuleNotFoundError: 没有名为“__main__.gtts”的模块; &#39;__main__&#39; 不是一个包 - ModuleNotFoundError: No module named '__main__.gtts'; '__main__' is not a package ModuleNotFoundError:没有名为&#39;__main __。web_app&#39;的模块; 从子文件夹导入包时,&#39;__ main__&#39;不是包 - ModuleNotFoundError: No module named '__main__.web_app'; '__main__' is not a package while importing package from sub folder Python 3 - 在同一目录中导入.py文件 - ModuleNotFoundError:没有名为'__main __。char'的模块; '__main__'不是包 - Python 3 - importing .py file in same directory - ModuleNotFoundError: No module named '__main__.char'; '__main__' is not a package 在docker中运行龙卷风python:ModuleNotFoundError:没有名为&#39;__main __。config&#39;的模块; &#39;__main__&#39;不是一个包 - running tornado python in docker: ModuleNotFoundError: No module named '__main__.config'; '__main__' is not a package 解决“ModuleNotFoundError: ...'__main__' is not a package”错误 - Working around "ModuleNotFoundError: ...'__main__' is not a package" error ModuleNotFoundError:__main__ 不是包是什么意思? - ModuleNotFoundError: What does it mean __main__ is not a package? 如何解决 Django 'ModuleNotFoundError' 没有名为 '___main___.models' 的模块 - How to solve Django 'ModuleNotFoundError' No module named '___main___.models' 错误:没有名为“__main__.forms”的模块; &#39;__main__&#39; 不是 Python3 中的包 - Error: No module named '__main__.forms'; '__main__' is not a package in Python3 Python pickle:ImportError:没有名为__main__的模块 - Python pickle: ImportError: No module named __main__
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM