找不到静态文件路径页面重定向到另一个页面

Posted

技术标签:

【中文标题】找不到静态文件路径页面重定向到另一个页面【英文标题】:Not able to find static file path page redirect to another page 【发布时间】:2015-07-13 17:32:08 【问题描述】:

第一次加载索引页面时,它会给出静态文件路径:

[04/May/2015 09:16:22] "GET / HTTP/1.1" 200 3536
[04/May/2015 09:16:22] "GET /static/css/bootstrap.min.css HTTP/1.1" 304 0
[04/May/2015 09:16:22] "GET /static/js/jquery.js HTTP/1.1" 304 0
[04/May/2015 09:16:22] "GET /static/js/bootstrap.min.js HTTP/1.1" 304 0
[04/May/2015 09:16:22] "GET /static/css/simple-sidebar.css HTTP/1.1" 304 

点击超链接后重定向到另一个页面静态文件路径更改为:

[04/May/2015 10:24:11] "GET /parselog/static/css/simple-sidebar.css HTTP/1.1" 404 2281
[04/May/2015 10:24:11] "GET /parselog/static/js/jquery.js HTTP/1.1" 404 2251
[04/May/2015 10:24:11] "GET /parselog/static/css/bootstrap.min.css HTTP/1.1" 404 2278
[04/May/2015 10:24:11] "GET /parselog/static/js/bootstrap.min.js HTTP/1.1" 404 2272

重定向到另一个页面后找不到静态文件路径。

settings.py:

STATIC_URL = '/static/'


STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(os.path.dirname(__file__),'../static'),
)

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(os.path.dirname(__file__),'../templates'),
)

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div class="row">
<div class="col-xs-6 col-sm-4" style='overflow:auto; width:400px; height:500px;'>
% for logfile in logfiles %
<a href="% url 'parselog' logfile %"> logfile </a>
<br>
% endfor %
</div>
</div>
</body>
</html>

urls.py:

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:

    url(r'^$', 'fuzeLogApp.views.home', name='home'),
    url(r'^parselog/(?P<logfile>[\w.-]+)/$', 'fuzeLogApp.views.parselog', name='parselog'),


    url(r'^admin/', include(admin.site.urls)),
)

【问题讨论】:

你能告诉我们你的解析日志模板吗?另外,您如何将静态文件添加到您的 index.html 中? 【参考方案1】:

试试这个:

STATICFILES_DIRS = (
    ...
    os.path.join(os.path.dirname(__file__),'static'),
)

TEMPLATE_DIRS = (
    ...
    os.path.join(os.path.dirname(__file__),'templates'),
)

【讨论】:

以上是关于找不到静态文件路径页面重定向到另一个页面的主要内容,如果未能解决你的问题,请参考以下文章

IntelliJ IDEA 文件夹重命名--解决重命名后js文件引用找不到路径报404错误

找不到子域时如何使网站重定向到页面[重复]

vue静态资源路径设置

Django 第二篇

下载文件并通过 ajax 将其重定向到另一个页面

如果在反应中找不到本地存储值,则重定向到特定页面