【问题标题】:error 404 (Not Found) when trying to get local JSON file with AJAX method on django project尝试在 django 项目上使用 AJAX 方法获取本地 JSON 文件时出现错误 404(未找到)
【发布时间】:2021-04-21 09:07:03
【问题描述】:

我正在尝试从 JS 文件访问我的数据库。为此,我将所有模型对象序列化为本地 JSON 文件,现在我尝试使用 AJAX 方法(我以前从未尝试过)读取这些文件,但找不到它们。

我的目录如下所示:

/RecipeSite
  (...)
  /reading
  /writing
    (...)
    /static/writing/
             /css
             /scss
             /js
               forms.js
             /json
               test.json
  (...)

我的设置.py:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

(...)

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, STATIC_URL),
    os.path.join("RecipeSite", "static"),
)

LOGIN_REDIRECT_URL = 'RecipeSite-home'

这是我的 RecipeSite/urls.py:

from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include, re_path
from django.views.generic import RedirectView
from users import views as users_views


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('reading.urls')),
    path('new_recipe/', include('writing.urls')),
    re_path(r'^Register/$', users_views.register, name='register'),
    re_path(r'^Login/$', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
    re_path(r'^Logout/$', auth_views.LogoutView.as_view(), name='logout'),
]

django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

我的阅读/urls.py:

from django.conf.urls import url
from django.views.generic import RedirectView
from django.urls import include, path, re_path

from . import views

urlpatterns = [
    path('', RedirectView.as_view(url='home_page/', permanent=True)),
    path('home_page/', views.index, name='RecipeSite-home'),
    re_path(r'(?P<r_Name>[\w.@+-]+)/(?P<r_Id>\d+)', views.about, name='recipe'),
]

我的写作/urls.py:

from django.conf.urls import url
from django.urls import path

from . import views

urlpatterns = [
    path('', views.writing, name='writing'),
]

在我的写作/js/forms.js 我有这个代码:

(...)
$.ajax({
      type: "GET",
      dataType: "json",
      url: "/test.json",
      success: function(data) {
        console.log(data);
      }
    });
(...)

但是当我运行它时,控制台显示 GET http://127.0.0.1:8000/test.json 404 (Not Found)

【问题讨论】:

  • 请显示您的网址

标签: json django ajax


【解决方案1】:

你可以使用:

urls.py

from django.views.generic.base import TemplateView

    path( "test.json",
        TemplateView.as_view(template_name="static/writing/test.json", content_type="application/json")),

但是,在生产过程中,Django does not serve static files,这将是您的网络服务器的工作,它应该并且能够提供 json 文件。

【讨论】:

  • 我正在使用 AJAX 方法,因为我想访问我的数据库而不必重新加载页面,加载整个模板对我不起作用
  • 视图仍会返回您需要的 json,但在生产中,您的 Web 服务器(例如 Nginx 和 Apache)可以更好地为文件提供服务。如果您决定采用 django 方式,您可能还需要在设置中将 static 添加到 TEMPLATE_DIRS
猜你喜欢
  • 2021-11-21
  • 2015-01-20
  • 2011-02-18
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 2014-01-27
相关资源
最近更新 更多