带有芹菜的django中存在TemplateDoesNotExist错误



我正在通过本教程学习将芹菜与django一起使用。在本教程中,该人员正在开发一个使用django和芹菜的网络抓取工具。我试图遵循教程,但我面临以下错误消息

TemplateDoesNotExist at /
home.html, scraping/products_list.html

就是这样排列我的文件的

.
├── celerybeat-schedule.db
├── celerydjangotutorial
│   ├── __init__.py
│   ├── asgi.py
│   ├── celery.py
│   ├── settings.py
│   ├── templates
│   │   ├── base.html
│   │   └── home.html
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
└── scraping
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   ├── 0001_initial.py
│   ├── 0002_auto_20200827_0735.py
│   ├── __init__.py
│   └── __pycache__
│       ├── 0001_initial.cpython-38.pyc
│       ├── 0002_auto_20200827_0735.cpython-38.pyc
│       └── __init__.cpython-38.pyc
├── models.py
├── tasks.py
├── tests.py
└── views.py

其中celerydjangotutorial是django项目,抓取是一个django应用程序。在本教程中,该人员将模板放置到项目目录中。他还在项目目录中创建了一个视图文件。我像他在教程里那样跟着他。这是我的密码。

celerydjangotutorial/uls.py

from django.contrib import admin
from django.urls import path, include
from .views import HomePageView
urlpatterns = [
path('', HomePageView.as_view(), name='home'),
path('admin/', admin.site.urls),
]

celerydjangotutorial/views.py

from django.shortcuts import render
from django.views import generic
from scraping.models import Products
class HomePageView(generic.ListView):
template_name = 'home.html'
context_object_name = 'products'
def get_queryset(self):
return Products.objects.all()

刮擦/模型.py

from django.db import models
class Products(models.Model):
title = models.CharField(max_length=200)
link = models.CharField(max_length=2083, default="", unique=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
website = models.CharField(max_length=30, default="", blank=True, null=True)

刮擦/任务.py

import time
import json
from datetime import datetime
from celery import Celery
from celery.schedules import crontab
from celery import shared_task
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.keys import Keys
@shared_task
def scrape():
try:
print('starting the scraping process')
product_list = []
url = f'https://www.amazon.com/s?k=chips&ref=nb_sb_noss_2'
driver = webdriver.Chrome('./chromedriver')
driver.get(url)
driver.implicitly_wait(15)
links = driver.find_elements_by_class_name("a-size-mini")
for link in links:
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.LINK_TEXT, link.text)))
product_meta = {
'link': element.get_attribute('href'),
'website': 'amazon'
}
product_list.append(product_meta)
print('scraping process completed')
return save_function(product_list)
except Exception as e:
print('scraping failed')
print(e)
@shared_task(serializer='json')
def save_function(product_list):
print('saving extracted data')
new_count = 0
for product in product_list:
try:
Products.objects.create(
title = product['title'],
link = product['link'],
website = product['website']
)
new_count += 1
except Exception as e:
print('failed at latest_product is None')
print(e)
break
return print('finished')

celerydjangotutorial/celery.py

from __future__ import absolute_import
import os
from celery import Celery
from celery.schedules import crontab
os.environ.setdefault('DJANGO_SETTINGS_MODULE','celerydjangotutorial.settings')
app = Celery('celerydjangotutorial')
app.conf.timezone = 'UTC'
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()

设置.py

...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'scraping.apps.ScrapingConfig'
]
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
...
CELERY_BROKER_URL = 'amqp://localhost:5672'
CELERY_RESULT_BACKEND = 'amqp://localhost:5672'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'UTC'

我不确定我做错了什么。我遵循了教程的大部分内容。除了tasks.py之外,其余代码与他的代码相似。请帮帮我。

提前感谢

[EDIT-1]公布整个错误

Internal Server Error: /
Traceback (most recent call last):
File "/Users/sashaanksekar/anaconda3/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/sashaanksekar/anaconda3/lib/python3.8/site-packages/django/core/handlers/base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/sashaanksekar/anaconda3/lib/python3.8/site-packages/django/core/handlers/base.py", line 143, in _get_response
response = response.render()
File "/Users/sashaanksekar/anaconda3/lib/python3.8/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/Users/sashaanksekar/anaconda3/lib/python3.8/site-packages/django/template/response.py", line 81, in rendered_content
template = self.resolve_template(self.template_name)
File "/Users/sashaanksekar/anaconda3/lib/python3.8/site-packages/django/template/response.py", line 63, in resolve_template
return select_template(template, using=self.using)
File "/Users/sashaanksekar/anaconda3/lib/python3.8/site-packages/django/template/loader.py", line 47, in select_template
raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: home.html, scraping/products_list.html

尝试在templates文件夹下添加应用程序名称,并将HTML文件移动到其中。所以应该是这样templates/celerydjangotutorial/base.htmlhome.html

检查文件、文件夹和路径的名称

相关内容

  • 没有找到相关文章

最新更新