代码之家  ›  专栏  ›  技术社区  ›  devmrh

NGNIX-Django 2-角6/角路由在重新加载页面后获得404

  •  2
  • devmrh  · 技术社区  · 6 年前

    它的Django和Angular6(使用静态文件)项目。刷新浏览器时出现404未找到错误。我几乎没有搜索,根据这一点, answer I did same thing this time angular route in refresh work(dont get 404)but I get server error that say following error*please see my uploaded image:。

    *角度静态文件位于静态文件夹中

    “分析http://robinlearn.com/api/v1/store/view-global-discount/时HTTP失败”
    艾斯
    名称:“httpErrorResponse”
    
    
    

    网址:py:

    urlpatterns=[
    URL(R’^(?)!ng/).*$”,homepageview.as_View(),name=“angular_app”)
    
    ]
    urlpatterns+=静态(base.static_url,document_root=base.static_root)+\
    静态(base.media_url,document_root=base.media_root)
    
    urlpatterns+=静态(base.angular_url,document_root=base.static_root)
    

    视图:

    class homepageview(模板视图):
    def get(self,请求,**kwargs):
    返回呈现(请求,'index.html',context=none)
    

    nginx配置:

    listen 80;
    服务器名称robinlearn.com;
    location=/favicon.ico访问注销;未找到注销;
    位置/静态/{
    根/家/生物/学院;
    }
    
    位置/{
    
    包括代理参数;
    #proxy_set_header主机$http_主机;
    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$方案;
    代理通行证http://unix:/home/bio/academy/academy.sock;
    }
    }
    

    设置:

    project_root=os.path.dirname(os.path.dirname((uu file_uuu)))
    #static_root=base_dir+'/academy/staticfiles'
    静态“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(uuu file_uuuuu)))
    #用于CollectStatic查找静态文件的额外位置。
    静态文件目录=(
    os.path.join(project_root,'static'),
    )
    

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

    %load static%
    &!文档类型HTML>
    <html lang=“fa”id=“persain”>
    <头部>
    <meta charset=“utf-8”>
    <title>罗宾</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>
    <正文>
    <app root>正在加载。..</app根目录
    <script type=“text/javascript”src=“%static”运行时.6afe30102d8fe7337431.js“%”></script>
    <script type=“text/javascript”src=“%static”polyfills.b0205464c9b4e7fe3b3.js“%”></script>
    <script type=“text/javascript”src=“%static”scripts.59ed76cc23ba77b0ec72.js“%”></script>
    <script type=“text/javascript”src=“%static”main.bfc87555f7931caf0a5b.js“%”></script>
    </body>
    </html>
    answer我这次在刷新工作中做了同样的事情(不要得到404),但是我得到了服务器错误,上面写着以下错误*请查看我上传的图片:

    *角度静态文件位于静态文件夹中

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

    full error

    网址.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(在template文件夹中)加载静态角度

    {% 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>
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Dmitrii G.    6 年前

    移动行URL(r'^(?!ng/).*$”,homepageview.as_view(),name=“angular_app”)位于最底部,而不是像现在这样位于顶层,因此Django将优先使用API URL,假定其中一行是您的API。