- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在创建一个用于网页抓取的应用程序目录,该目录是在我的 django_project 中抓取的。我在将 models.py 模块中的类导入到views.py 模块中时遇到错误。
这是我的项目结构:
这是我在 scrape 应用程序中 models.py 中的代码
from django.db import models
# Create your models here.
# model -- headline (title, url, date)
class Headline(models.Model):
title = models.CharField(max_length=120)
url = models.TextField()
event_date = models.TextField()
def __str__(self):
return self.title
以及抓取应用程序中views.py内的这段代码
from django.shortcuts import render, redirect
import requests
requests.packages.urllib3.disable_warnings()
from bs4 import BeautifulSoup
from .models import Headline
# Create your views here.
def scrape():
# Use the session to get the URL
session = requests.Session()
url = 'https://allevents.in/malacca/all?ref=cityhome-popular'
# Content is basically grabs all the HTML that comes from the session
content = session.get(url, verify=False).content
soup = BeautifulSoup(content, "html.parser")
# Return a list
#item = soup.find_all('div', class_="event-item")
for item in soup.find_all('div', class_="event-item"):
title = item.find("h3").text.strip()
linkhref = item.find("h3").find("a").get('href')
date_posted = item.find("div", {"class":"right"})
new_headline = Headline()
new_headline.title = title
new_headline.url = linkhref
new_headline.event_date = date_posted
new_headline.save()
return redirect('/event/')
尝试从 cmd 运行 python views.py 后出现此错误
Traceback (most recent call last):
File "views.py", line 5, in <module>
from .models import Headline
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package
我也在我的views.py中尝试了这个
from scrape.models import Headline
但我收到以下错误
Traceback (most recent call last):
File "views.py", line 5, in <module>
from scrape.models import Headline
File "C:\Users\USER\django_project\scrape\scrape.py", line 2, in <module>
from .models import Headline
ImportError: attempted relative import with no known parent package
如果我从模型导入标题更改
Traceback (most recent call last):
File "views.py", line 6, in <module>
from models import Headline
File "C:\Users\USER\django_project\scrape\models.py", line 7, in <module>
class Headline(models.Model):
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\apps\registry.py", line 134, in check_apps_ready
settings.INSTALLED_APPS
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__
self._setup(name)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\conf\__init__.py", line 64, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
最佳答案
当您想要导入 model
时在本例中,您应该使用 models.py
的路径明确 Python 应该查看的位置。然后,您可以通过指定类名来导入一个或多个模型,如下所示:
from <path_to_models.py> import <your_model_name>
要解决您的问题,请更改:
from .models import Headline
到
from models import Headline
在您导入 Headline
的所有文件中型号
编辑
当您使用具有自动填充功能的 IDE 时,例如 Pycharm。您可以通过简单的方式导入模型:
alt + enter
import model
关于python - 我遇到错误 "ModuleNotFoundError: No module named ' __main__.models'; '__main__' 不是一个包”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57015358/
我有一堆字符串,其中包含以姓氏,名字格式命名的名称列表,并用逗号分隔,如下所示: names 序列化字符串中的“first name last name”,我们在Stack Overflow上找到一个
在尝试合并多个数据框时,我得到了一些非常奇怪的东西。帮助! 我需要通过“RID”和“VISCODE”列合并一堆数据框。这是它的外观示例: d1 = data.frame(ID = sample(9,
让我们尽可能简单地做第一个例子。 我想知道如何应用这个宏;像这里适用于例如printf("%s",macro(arg)); #include #include #include #define
以下2个bean声明之间有什么区别吗? @Bean(name = "bean1") public A getA() { return new A(); } @Bean @Quali
if(c.get_name(&name) && name && !strcmp(name, contName)) 谁能告诉我这行代码在 C++ 中的含义 最佳答案 如果 c 有一个名字并且它等于 co
我是 Rails 的初学者,在改进我的搜索查询时遇到了一些问题: 在我调用的 Controller 中: def index if params[:search] @persons = Pers
谁能帮我解决这段代码的最后一部分的编译错误它的说法创建构造函数请帮助 public class Officer { public static void main(String args[]
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve t
我创建了一个 Web 应用程序,它具有使用 JSF 技术的 createBook.xhtml 并且它使用托管 Bean。在此页面中,用户必须在输入字段中填写所需信息,然后点击提交按钮。然后应使用 do
嘿,我正在尝试弄清楚如何将在 mySQL 中工作的语句转换为 PostgreSQL 并且很好奇是否有人知道解决方案。 这是在 mySQL 中有效的语句: def self.by_name(keywor
如果我要创建所有类型的类型,而不是使用字符串和原始类型,那么最大的缺点是什么? 通常它看起来像: String name = person.getName(); int age = person.ge
我正在尝试从以下内容中提取郊区名称: 12 street name, suburb name, CTG 1234 在 PHP 中使用正则表达式。 街道和郊区名称都可以是任意数量的单词长度。 CTG 是
我有一个呈现个人详细信息组件的父组件,并且正在注入(inject)父组件的验证器范围。如果我使用 v-validate 指令和 this.$validator.validateAll() 或 this
现在,据我了解,name[] 声明中的 extern 告诉编译器它的定义在其他地方(在我的程序中,我定义了它低于我使用它的部分)。但是为什么 strlen() 和 sizeof 会有不同的结果?str
我一直在解决一个问题: "Design a program that asks the user for a series of names (in no particular order). Aft
我的 XML 如下所示: 我想使用 JAXB 来阅读该内容。 我知道我能做到 @XmlRootElement(name="thing") public class Thing{
对于字符串 name[],我们可以使用 strlen(name)+1 和 sizeof(name) 互换 在我们的代码中没有经过深思熟虑?他们不一样吗?我检查了一下,发现两者的返回类型都是相同的,si
我正在尝试从以下内容中提取郊区名称: 12 street name, suburb name, CTG 1234 在 PHP 中使用正则表达式。 街道和郊区名称的长度可以是任意数量。 CTG 是 st
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve t
bash 手册列出了 for 的语法。复合语句为for name [ [ in [ word ... ] ] ; ] do list ; done这意味着 do 之前的分号如果 in 是可选的子句省略
我是一名优秀的程序员,十分优秀!