【问题标题】:ngnix - django 2 -angular 6 / angular routing get 404 after reload pagenginx - django 2 -angular 6 / 角度路由在重新加载页面后得到 404
【发布时间】:2018-12-30 19:16:44
【问题描述】:

它的 django 和 angular 6(使用静态文件)项目。当我刷新浏览器时,我得到 404 not found 错误 .. 我几乎没有搜索,根据这个 answer 我这次在刷新工作中做了同样的事情(不要得到 404)但是我得到服务器错误,说下面的错误 *请看我上传的图片:

*角度静态文件在静态文件夹中

 "Http failure during parsing for http://robinlearn.com/api/v1/store/view-global-discount/"
​
name: "HttpErrorResponse"

url.py:

   urlpatterns = [
        url(r'^(?!ng/).*$', HomePageView.as_view(),name="angular_app")

    ]
    urlpatterns += static(base.STATIC_URL, document_root=base.STATIC_ROOT) + \
               static(base.MEDIA_URL, document_root=base.MEDIA_ROOT)

urlpatterns += static(base.ANGULAR_URL, document_root=base.STATIC_ROOT)

查看:

 class HomePageView(TemplateView):
    def get(self, request, **kwargs):
        return render(request, 'index.html', context=None)

nginx 配置:

    listen 80;
    server_name robinlearn.com;
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/bio/academy;
    }

    location / {

        include proxy_params;
       #proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://unix:/home/bio/academy/academy.sock;
    }
}

设置:

PROJECT_ROOT = os.path.dirname(os.path.dirname((__file__)))
#STATIC_ROOT = BASE_DIR+'/academy/staticfiles'
STATIC_URL = '/static/'

STATIC_ROOT = os.path.join((BASE_DIR),"static")
#MEDIA_ROOT =  os.path.join(os.path.dirname(BASE_DIR),"media")

SS_ROOT = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

这是我加载静态角度的 index.html(在模板文件夹中)

{% load static %}
<!DOCTYPE html>
<html lang="fa" id="persain">
<head>
  <meta charset="utf-8">
  <title>robin</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="{% static 'favicon.png' %}">
<link rel="stylesheet" href="{% static 'styles.9be16346d4b858749d38.css' %}"></head>
<body>
  <app-root>Loading .  .  .</app-root>
<script type="text/javascript" src="{% static 'runtime.6afe30102d8fe7337431.js' %}"></script>
<script type="text/javascript" src="{% static 'polyfills.b0205464c9bd4e7fe3b3.js' %}"></script>
<script type="text/javascript" src="{% static 'scripts.59ed76cc23ba77b0ec72.js' %}"></script>
<script type="text/javascript" src="{% static 'main.bfc87555f7931caf0a5b.js' %}"></script>
</body>
</html>

【问题讨论】:

  • 您的后端 API 端点不工作。 Django 正在返回 index html 页面。修复你的路线
  • 你需要有你的基础根'/'来服务索引html页面和'/api/....'来服务后端逻辑
  • 怎么样?你能把你的解决方案写成答案吗?
  • 这是不是你的代码?您可以首先将行 url(r'^(?!ng/).*$', HomePageView.as_view(),name="angular_app") 移动到最底部而不是像现在这样的顶层,这样假设这些行之一是您的 API,Django 会优先考虑 API url
  • 是的,现在已经修复了

标签: django angular nginx django-rest-framework django-staticfiles


【解决方案1】:

将 url(r'^(?!ng/).*$', HomePageView.as_view(),name="angular_app") 行移动到最底层,而不是像现在这样的顶层,这样假设这些行之一是您的 API,Django 会优先考虑 API url

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2019-05-05
    • 2017-10-22
    • 2019-02-12
    相关资源
    最近更新 更多