无法找到静态文件路径页面重定向到另一个页面 [英] Not able to find static file path page redirect to another page

查看:406
本文介绍了无法找到静态文件路径页面重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当首次加载索引页面时,会给出静态文件路径:

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

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

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

重定向到另一页后无法找到静态文件路径。



settings.py

  STATIC_URL ='/ static /'


STATICFILES_DIRS =(
#将字符串放在这里, / home / html / static或C:/ www / django / static。
#总是使用正斜杠,甚至在Windows上。
#不要忘记使用绝对路径,而不是相对路径。
os.path.join(os.path.dirname(__ file __),'.. / static'),


TEMPLATE_DIRS =(
#在这里,像/ home / html / django_templates或C:/ www / django / templates一样。
#总是使用正斜杠,甚至在Windows上
#不要忘记使用绝对路径,不是相对路径
os.path.join(os.path.dirname(__ file __),'.. / templates'),

pre>

index.html

 <!DOCTYPE html> 
< html lang =en>
< head>
< / head>
< body>
< div class =row>
< div class =col-xs-6 col-sm-4style ='overflow:auto;宽度:400像素;高度:500像素;>
{%loglog in logfiles%}
< a href ={%url'parselog'logfile%}> {{logfile}}< / a>
< br>
{%endfor%}
< / div>
< / div>
< / body>
< / html>

urls.py

 从django.conf.urls导入模式,包括,url 

从django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
#示例:

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)),


解决方案

尝试这样:

 code> STATICFILES_DIRS =(
...
os.path.join(os.path.dirname(__ file __),'static'),


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


When first time index page load it give static file path:

[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 

After click on hyperlink it redirect to another page static file path change to:

[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

After redirect to another page it's not able to find the static file path.

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)),
)

解决方案

Try this:

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

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

这篇关于无法找到静态文件路径页面重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆