我正在创建一个用于Web抓取的应用程序目录,该目录在我的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.
答案 0 :(得分:0)
要导入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