nginx - django 2 -angular 6 / angular 路由在重新加载页面后得到 404
ngnix - django 2 -angular 6 / angular routing get 404 after reload page
它的 django 和 angular 6(使用静态文件)项目。当我刷新浏览器时,我收到 404 not found 错误。我几乎没有搜索,根据这个 answer 这次我做了同样的事情 angular 刷新工作中的路由(没有收到 404),但我收到服务器错误说下面的错误 *请看我上传的图片 :
*angular 静态文件在静态文件夹中
"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(在模板文件夹中)加载静态 angular
{% 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>
将行 url(r'^(?!ng/).*$', HomePageView.as_view(),name="angular_app") 移到最底部而不是像你现在拥有的顶层,这样 Django 会优先考虑 API urls 假设其中一行是你的 API
它的 django 和 angular 6(使用静态文件)项目。当我刷新浏览器时,我收到 404 not found 错误。我几乎没有搜索,根据这个 answer 这次我做了同样的事情 angular 刷新工作中的路由(没有收到 404),但我收到服务器错误说下面的错误 *请看我上传的图片 :
*angular 静态文件在静态文件夹中
"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(在模板文件夹中)加载静态 angular
{% 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>
将行 url(r'^(?!ng/).*$', HomePageView.as_view(),name="angular_app") 移到最底部而不是像你现在拥有的顶层,这样 Django 会优先考虑 API urls 假设其中一行是你的 API