25

私はまだ django を使い始めたばかりで、CSS の動作に問題があります。
リンクからの指示に従いました: Django Static Link tutorial、静的ファイルの処理について。しかし、それはまだ機能していません。

設定

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/Users/a9austin/Development/sites/AlphaSocks/src/static_root/'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
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.
'/Users/a9austin/Development/sites/AlphaSocks/src/staticfiles'

)

見る

#from django.http import HttpResponse
from django.shortcuts import render_to_response


def index(request):
return render_to_response('index.html')


index.html

<link rel="stylesheet" href="{{STATIC_URL}}css/style.css" type="text/css" media="screen" >

そしてディレクトリ編成

src->staticfiles->css->style.css



どうもありがとう、あなたの助けと時間をありがとう!

4

9 に答える 9

8

キャッシュをクリアしてみてください。Google Chrome を使用している場合は、設定 > 閲覧データの消去 > キャッシュされた画像とファイルの消去を選択し、データの消去をクリックします。

于 2021-02-03T15:23:07.707 に答える
1

RequestContext を応答に追加すると、STATIC_URL 変数がテンプレートに読み込まれます。

変更してみてください:

from django.shortcuts import render_to_response

def index(request):
    return render_to_response('index.html')

に:

from django.shortcuts import render_to_response
from django.template.context import RequestContext

def index(request):
    return render_to_response("index.html", context_instance=RequestContext(request)) 

詳細については、テンプレート内の静的ファイルの参照に関する Django ドキュメントを 参照してください。

于 2012-11-19T02:05:58.370 に答える
1

コーディングに問題がなく、エラーが表示されない場合。次に、これを実行して、問題の解決を試みることができます。

キャッシュをクリアします:

Google chrome を使用している場合は、設定に移動します --> 閲覧データを消去します --> キャッシュされた画像とファイルを消去する を選択し、データを消去をクリックします

于 2021-03-26T18:20:59.883 に答える
0

これが開発モードで発生している場合は、ファイルに設定DEBUG=Trueしてください。settings.pyまた、 と が次のようにファイルに設定されていることを確認しMEDIA_URLてくださいMEDIA_ROOTsettings.py

MEDIA_URL  = '/mymediafolder/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mymediafolder')

次に、メインの urls ファイルmyapp/urls.pyに次のものが必要です。

from django.conf.urls import url, include
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    #Your url patterns here
]

urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

staticfiles_urlpatterns()、開発モードで静的ファイルを提供するために使用されます。

于 2021-05-19T20:25:41.160 に答える
-4
There is an easy way if you feel that your CSS isn't working.

プロジェクトがそれほど大きくない場合は、HTML と同じファイルに CSS ファイルを作成できます。そして、それを実行します。そのようにして、たとえば実行されます

`

<head>
  <meta charset="UTF-8">
  <title>Promantus Bot</title>
      <style type="text/css"> 
* {
    margin: 0;
    padding: 0;
}

body {
    background-color:#FF625F;
}

h1, p {
    font-family: sans-serif;
    text-align: center;
    color: #323330;
    font-size:  100px;
}


p {
    font-size: 30px;
}

#output, #container {
    display: flex;
    justify-content: center;
    margin-top: 100px;
}


input {
    background-color: #eee;
    border: none;
    font-family: sans-serif;
    color: #000;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 30px;
}






</style>


</head>

<body>

  <div id="output"></div>

<div id="container">
    <input type="text" id="input" value="">
</div>





</body>

</html>
`
It's going to run fine this way.
于 2019-06-28T09:10:38.340 に答える