0

私はしばらくの間この問題を抱えていました.CSSファイルをあちこちに移動して設定を変更しようとしており、メディアのURLをいじっています.他の質問を読んでください。

私の CSS ファイルは /home/justin/test/static/ ディレクトリにあります。テンプレートは /test/templates ディレクトリにあります。

私の設定は次のとおりです。

STATIC_ROOT = '/home/justin/test/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    '/home/justin/test/static/',
)

私のURLは次のとおりです。

urlpatterns = patterns('',

    url(r'^$', 'views.home'),

    # Static Files
    url(r'^static/(?P<path>.*)$','django.views.static.serve', {'document_root':settings.MEDIA_ROOT}),

)

メイン テンプレートには 3 つすべてが含まれています。

<link rel="stylesheet" href="/static/style.css" />
<link rel="stylesheet" href="{{ STATIC_URL }}style.css" />
<link rel="stylesheet" href="{{ STATIC_URL }}/style.css" />

彼らは出てきます:

<link href="/static/style.css" rel="stylesheet"></link>
<link href="style.css" rel="stylesheet"></link>
<link href="/style.css" rel="stylesheet"></link>

最初は正しいファイルに出てきますが、うまくいきません。どんな助けでも本当に感謝しています。ありがとうございました。

私のviews.py:

from django.shortcuts import render_to_response
def home(request):
    return render_to_response('index.html',{})

端末からの私のエラーは次のとおりです。

[16/Aug/2013 21:00:21] "GET /static/style.css HTTP/1.1" 500 1729
4

5 に答える 5

0

テンプレートでこのタグ ({% load static from staticfiles %}) を使用する必要があります。

{% load static from staticfiles %}
<link rel="stylesheet" type="text/css"  href="{% static 'style.css' %}" />
于 2013-08-17T04:30:04.423 に答える
0

開発モード (つまり)DEBUG=Truesettings.py、静的ファイルを提供する

次の行を入れるだけですurls.py

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf here ...
urlpatterns += staticfiles_urlpatterns()

本番モードでは、django の前で nginx/apache を使用して静的ファイルを提供する必要があります。

参照: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

あなたの質問には次の行があります:

    url(r'^static/(?P<path>.*)$','django.views.static.serve', {'document_root':settings.MEDIA_ROOT}),

通常、settings.MEDIA_ROOTはユーザーがアップロードしたファイル用で、 とは別のフォルダですsettings.STATIC_ROOT

于 2013-08-17T06:47:51.393 に答える