【发布时间】: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)
【问题讨论】:
-
请显示您的网址