Django在第三方API之后再也找不到base.html了

2024-05-18 05:12:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的Django应用程序中调用第三方API,更具体地说是在views.py中。一旦我做了这个调用,Django就找不到模板base.html

基本上就是这样:

  1. 用户在搜索表单中搜索术语
  2. 请求被发送到我的视图
  3. 当用户发出GET请求时,视图有一个if语句要处理,因此这段逻辑处理请求
  4. 视图调用我编写的一个单独的函数search_by_jobsite()
  5. search_by_jobsite()内部,对第三方API进行了调用,我将.pkl文件的位置传递给API。位置是/webapp/camera_search/api/pickle/config.pkl
  6. 一切正常,search_by_jobsite()检索必要的信息
  7. 它将信息返回给视图,视图继续定义上下文字典并尝试render(request, 'siren_search.html', context)。然而,这就是它失败的地方

siren_search.html中的第一行是{% extends 'base.html' %},因此它开始查找base.html,但它在错误的目录中查找。当base.html位于/webapp/webapp/templates/base.html时,它正在/webapp/camera_search/api/pickle/中搜索base.html。出于某种原因,当django转到pickle/文件夹将信息发送给第三方API时,它也开始在那里搜索base.html,但它可以找到siren_search.html,只需找到

django正在做的事情总结:

  1. /webapp/camera_search/views.py开始并进行第三方API调用
  2. 在API调用期间,django移动到/webapp/camera_search/api/pickle/
  3. 返回视图/webapp/camera_search/views.py
  4. 尝试呈现siren_search.html,并在正确的位置/webapp/camera_search/templates/siren_search.html找到它
  5. 试图扩展base.html并在/webapp/camera_search/api/pickle/中搜索,但它实际上位于/webapp/webapp/templates/base.html

以下是完整回溯的链接:http://dpaste.com/3ZTBJAG

我的问题是,发生了什么?我如何将Django重新路由回正确的目录

编辑:

这是我的模板设置和来自views.py的相关代码

# /webapp/webapp/settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['webapp/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',
            ],
        },
    },
]
# /webapp/camera_search/views.py

from .models import CameraSystem, JobSite
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render, redirect
from django.conf import settings
from zcrmsdk import ZCRMRecord, ZCRMRestClient, ZCRMModule

def siren_search(request):

    term = request.GET.get('query', '')
    context = {}

    #
    # ...
    #

    # Handle when the user presses enter
    elif 'query' in request.GET and term != '' and not request.is_ajax():
        try:
            deployed = search_by_jobsite('JOBSITE') # --- This is what i'm attempting to do. Currently the search works using the below functionality, but i'm trying to integrate this third party API call.
            jobsite = JobSite.objects.get(name__iexact=term)
            cameras = jobsite.camerasystem_set.all()
            context = {
                'cameras': cameras,
            }

        except ObjectDoesNotExist:
            context = {}
            pass

        return render(request, 'siren_search.html', context) # Django fails here

def search_by_jobsite(jobsite):
    # config is a dictionary with API authentication settings.
    config = settings.ZOHO_CONFIG 
    client = ZCRMRestClient.initialize(config)
    all_cameras = ZCRMModule.get_instance('Vendors').search_records(f'{jobsite}')
    deployed_cameras = {}

    # ... process data

    return deployed_cameras

下面是一个示例配置字典,我正在传递给API客户机

config = {
    'sandbox': 'False',
    'applicationLogFilePath': '/Users/user/Programming/suitee/webapp/camera_search/api/log/',
    'client_id': 'xxxx.xxxxxxxxxxxxx',
    'client_secret': 'xxxxxxxxxxxxxxxxxxxxxxxxx',
    'redirect_uri': 'http://localhost:8000/',
    'accounts_url': 'https://accounts.zoho.com',
    'token_persistence_path': '/Users/user/Programming/suite/webapp/camera_search/api/pickle/',
    'currentUserEmail': 'email@domain.com'
}

Tags: djangopy视图apisearchbaserequesthtml