2016-03-23 12 views
0

Hallo Ich bin sehr verwirrt über statische Dateien setzen. Alles funktioniert gut (zeigt Bild, Javascript, CSS), egal was ich versuche. Also bin ich verwirrt, welcher der Richtige ist.korrekte statische Dateien Einstellung

Derzeit ist dies, wie mein Projekt aussieht

project 
--project 
---------static 
---------media 
--env 
--static 
--------media 
--------static 

Und dies ist mein Code

MEDIA_URL = '/media/' 
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media") 

STATIC_URL = '/static/' 
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),) 
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static") 
# List of finder classes that know how to find static files in 
# various locations. 
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
# 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
) 

Wenn ich Python manage.py collectstatic tun, ich habe keine Fehler erhalten, aber statisch Ordner, der sich im äußeren statischen Ordner befindet, enthält nichts. Der Medienordner im statischen Ordner enthält jedoch die Dateien im Medienordner im Projektordner.

Auch Ich habe dies für aws,

AWS_FILE_EXPIRE = 200 
AWS_PRELOAD_METADATA = True 
AWS_QUERYSTRING_AUTH = True 

DEFAULT_FILE_STORAGE = 'project.utils.MediaRootS3BotoStorage' 
STATICFILES_STORAGE = 'project.utils.StaticRootS3BotoStorage' 
AWS_STORAGE_BUCKET_NAME = 'realproject' 
S3DIRECT_REGION = 'ap-northeast-2' 
S3_URL = '//%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME 
MEDIA_URL = '//%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME 
MEDIA_ROOT = MEDIA_URL 
STATIC_URL = S3_URL + 'static/' 
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/' 

import datetime 

date_two_months_later = datetime.date.today() + datetime.timedelta(2 * 365/12) 
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00 GMT") 

AWS_HEADERS = { 
    'Expires': expires, 
    'Cache-Control': 'max-age=86400', 
} 

Kann mir bitte jemand sagen, wenn ich es richtig mache?

übrigens, ich lese https://docs.djangoproject.com/en/1.9/howto/static-files/ und folgte ihm, ich bin mir nicht sicher, ob ich es richtig verfolgt habe (oben angezeigt), weshalb ich frage.

Antwort

0

Der Befehl sucht nach allen statischen Verzeichnissen und kombiniert diese Dateien in dem Verzeichnis, das durch die STATIC_ROOT Einstellung definiert wurde.

In Ihrem Fall ist STATIC_ROOT auf os.path.join(os.path.dirname(BASE_DIR), "static", "static") gesetzt, das heißt

your_project/static/static 

So ist dies, wo die statischen Dateien gesammelt werden. Wenn Sie sie im äußeren statischen Verzeichnis speichern möchten, können Sie STATIC_ROOT in os.path.join(os.path.dirname(BASE_DIR), "static") ändern.

Es gibt eine gute Diskussion darüber in der ausgezeichneten Django docs here.

Es ist ziemlich viel zu nehmen in diesen Einstellungen in, ist so hier eine kurze Zusammenfassung der einzelnen statischen Einstellung als Beispiel:

# this is the URL that django will look for static resources at 
# - i.e. http://your_domain/static 
# so this one is a URL used when by your web server and in template 
# shortcuts. 
STATIC_URL = '/static/' 

# this is where Django will look for static files to collect. 
# I.e. the search locations that collectstatic uses. 
# 'my_project/static' in this instance. You need to add the places 
# you write your static files to this directory. For example, if you 
# have several places where you are writing css files, add their 
# container directories to this setting. 
# it is a list of places to look for static files. 
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),) 

# this is where collectstatic will collect the static files to. 
# When you hook this all into your webserver, you would tell your 
# webserver that the /static/ url maps to this directory so that 
# your app can find the static content. It's a directory in your 
# project usually. 
# it's a single directory where the static files are collected together. 
STATIC_ROOT 
+0

ja danke diesem Grund habe ich verwirrt war, dann wie kommt mein Mediendateien werden in einem Ordner in my_project/static/media gesammelt. –

+0

Dies liegt an Ihrer MEDIA_ROOT-Einstellung, die lautet: MEDIA_ROOT = os.path.join (os.path.dirname (BASE_DIR), static, media)) 'ie' my_project/static/media'. Auch diese Einstellung führt dazu, dass sie irgendwo anders enden. – srowland

+0

Ich bin gerade so confusued, ich lese mehr Tutorials zu diesem mehr verwirrt ich bekomme. Ich habe versucht, zu ändern, wie Sie sagten, aber statische Dateien werden nicht in statische Ordner gesammelt. Ich werde einfach lassen Sie es wie MEDIA_URL = '/ media /' MEDIA_ROOT = os.path.join (os.path.dirname (BASE_DIR), "statisch", "Medien") STATIC_ROOT = '' STATIC_URL = '/ static /' STATICFILES_DIRS = (os.path.join (BASE_DIR, 'static'),) –