reactjs 当我运行react脚本'yarn start'时,index.html中到manifest.json的链接有效,但当我运行'python3 manage.py runserver'时无效,

o7jaxewo  于 11个月前  发布在  React
关注(0)|答案(4)|浏览(85)

当我运行'yarn start'时,我的index.html文件中的manifest.json链接工作正常,但当我运行'python3 manage.py runserver'时,我在终端中得到的是:

Not Found: /manifest.json
"GET /manifest.json HTTP/1.1" 404 2234

字符串
这也发生在我所有的静态链接和导入上。我对Django和React以及整个编程都很陌生,所以我认为我只是缺少一些简单的东西,但我无法弄清楚。
我一直在尝试使用{% load static %},但链接不起作用,即使我在settings.py中编辑STATIC_URL指向我的manifest.json目录。我也试图编辑view.pyurls.py,但我得到的只是终端中的语法错误。除此之外,我一无所知。
frontend/public/index.html

<html>

    <head>

    <title>WebProject</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="UTF-8">

    <link rel="manifest" href="manifest.json"/>

    </head>

    <body style="background-color: #FAF0E6; font-family: Verdana; font-size: 40px;">
    <div id="root"></div>

    </body>

</html>


前端/urls.py

from django.urls import path
from . import views
from django.conf.urls.static import static

urlpatterns = [
    path('', views.index),

]


前端/views.py

from django.shortcuts import render

def index(request):
    return render(request, 'frontend/public/index.html')


我希望我的浏览器能够正确加载manifest.json,沿着任何其他链接或导入,但我总是得到一个空白页面。
我在Django内部使用React,所以当我试图导入我的index.js时,同样的“Not Found”终端错误弹出。我假设如果我解决了manifest.json问题,我也会解决我的其他导入和链接问题。

rsl1atfo

rsl1atfo1#

我也遇到了同样的问题,找到了这个Can't get rid of missing manifest.json error
好吧,在我的情况下,这是浏览器缓存相关的,切换到隐身模式就足够了。

wi3ka0sx

wi3ka0sx2#

同样的事情也发生在我身上(空白页面,无法加载manifest.json + react build的静态文件),多亏了这个出色的article,我解决了这个问题
解决方案->假设你的react应用程序(build等)在一个名为frontend的文件夹中,与你的django项目处于同一级别,在你的settings.py文件中,你需要确保你的STATICFILES_DIRS变量设置如下(不要忘记尾部的逗号,因为它是一个元组)。

STATICFILES_DIRS = (
    os.path.join(os.path.join(BASE_DIR, 'frontend'), 'build', 'static'),
)

字符串

toiithl6

toiithl63#

在urls.py:
从django.urls导入re_path
更改:urlpatterns = [ ... path('',TemplateView.as_view(template_name ='index.html')]
收件人:
[ ... re_path('. xml',TemplateView.as_view(template_name ='index.html')]
我也犯了同样的错误,为我工作。

vvppvyoh

vvppvyoh4#

在我的情况下,我不得不将应用程序与BrowserRouter Package 在Index.js文件中

<BrowserRouter>
    <App />
  </BrowserRouter>

字符串

相关问题