2015-11-26 214 views
0

我試圖按照名爲Managing static files的Django文檔中的部分進行操作。我有興趣發現:在Django開發中不起作用的靜態文件

  1. 我在做什麼錯了。
  2. 或者,Django文檔有什麼問題。

1)確保django.contrib.staticfiles包含在你的INSTALLED_APPS。

在默認settings.py文件:

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', #<****HERE***** 
) 

2)在你的設置文件,定義STATIC_URL,例如:STATIC_URL = '/static/'

在默認settings.py

STATIC_URL = '/static/' 

3)在你的模板,無論是硬編碼像/static/my_app/myexample.jpg

在mysite2/mysite2 views.py /網址:

from django.http import HttpResponse 

def index(request): 
    return HttpResponse(''' 

     <h2>Hello</h2> 
     <img src="/static/mysite2/Aerial03.jpg"> 


    ''') 

4)Store中的一個文件夾中的靜態文件名爲靜態在您的應用程序。例如:my_app/static/my_app/myimage.jpg

這裏是我的目錄結構:

(django186p34)~/django_projects$ tree mysite2 

mysite2 
├── db.sqlite3 
├── manage.py 
└── mysite2 
    ├── __init__.py 

    ├── settings.py 
    ├── static   #<****HERE***** 
    │   └── mysite2 
    │    └── Aerial03.jpg 
    ├── urls.py 
    ├── views.py 
    └── wsgi.py 

4 directories, 13 files 

但是在啓動服務器後:

(django186p34)~/django_projects/mysite2$ python manage.py runserver 
Performing system checks... 

System check identified no issues (0 silenced). 
November 26, 2015 - 02:44:57 
Django version 1.8.6, using settings 'mysite2.settings' 
Starting development server at http://127.0.0.1:8000/ 
Quit the server with CONTROL-C. 

然後在我的瀏覽器導航到:

http://localhost:8000/ 

我看到的文字:

Hello 

,但圖像沒有發現:

[錯誤]無法加載資源:服務器與404 http://localhost:8000/static/mysite2/Aerial03.jpg

狀態迴應,我在我的HTML並沒有什麼作品嚐試過的URL的多種組合。

這裏是settings.py的全部(我不需要做任何改變):

""" 
Django settings for mysite2 project. 

Generated by 'django-admin startproject' using Django 1.8.6. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.8/topics/settings/ 

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.8/ref/settings/ 
""" 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
import os 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = 'pmaup3%)m09cs2goldduw2iogso%(#8cz0s-zmr%*e' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 


# Application definition 

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
) 

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'django.middleware.security.SecurityMiddleware', 
) 

ROOT_URLCONF = 'mysite2.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'mysite2.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 


# Internationalization 
# https://docs.djangoproject.com/en/1.8/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.8/howto/static-files/ 

STATIC_URL = '/static/' 

回答

0

這個固定的事情:

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'mysite2', #<*****HERE***** 
) 

如果你整理東西,讓你有內部應用程序的項目,例如:

(django186p34)~/django_projects$ tree mysite3 
mysite3 
├── db.sqlite3 
├── manage.py 
├── myapp 
│   ├── __init__.py 

│   ├── admin.py 
│   ├── migrations 
│   │   

│   │   
│   ├── models.py 
│   ├── static 
│   │   └── myapp 
│   │    └── Aerial03.jpg 
│   ├── tests.py 
│   ├── urls.py 
│   └── views.py 
└── mysite3 
    ├── __init__.py 

    ├── settings.py 
    ├── urls.py 
    └── wsgi.py 

8 directories, 24 files 

然後在settings.py添加您的應用程序名稱,INSTALLED_APPS

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'myapp', #<*****HERE***** 
) 

其他的一切在默認settings.py保持不變。然而,不同的網址:

mysite3/mysite3/urls.py:

from django.conf.urls import include, url 
from django.contrib import admin 

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

    url(r'^myapp/', include('myapp.urls')), 
] 

mysite3/MYAPP/urls.py:

from django.conf.urls import url 

from . import views 

urlpatterns = [ 
    url(r'^$', views.index, name='index'), 
] 

並在瀏覽器,網址變爲:

http://localhost:8000/myapp/ 

的myapp/views.py:

from django.shortcuts import render 

# Create your views here. 

from django.http import HttpResponse 


def index(request): 
    return HttpResponse(''' 

     <h2>Hello</h2> 
     <img src="/static/myapp/Aerial03.jpg"> 


    ''') 
相關問題