【问题标题】:css files are not being served by apache - djangoapache-django 不提供 css 文件
【发布时间】:2015-02-12 13:56:42
【问题描述】:

我想在 Ubuntu 13.10 上使用 Apache 2.4 和 mod_wsgi 配置和运行 Django。 除了css,一切都有效。我究竟做错了什么?

我通过 https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/modwsgi/ 创建了我的 /etc/apache2/sites-available/mysite.conf 文件

WSGIScriptAlias / /home/evgeny/django_progects/django_show_table/table/wsgi.py
WSGIPythonPath /home/evgeny/django_progects/django_show_table

Alias /static/ /home/evgeny/django_progects/django_show_table/static/

<Directory home/evgeny/django_progects/django_show_table/static>
Require all granted
</Directory>

<Directory /home/evgeny/django_progects/django_show_table/table>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

settings.py

2   Django settings for table project.
3   
4   For more information on this file, see
5   https://docs.djangoproject.com/en/1.7/topics/settings/
6   
7   For the full list of settings and their values, see
8   https://docs.djangoproject.com/en/1.7/ref/settings/
9   """
10  
11  # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
12  import os
13  BASE_DIR = os.path.dirname(os.path.dirname(__file__))
14  
15  TEMPLATE_CONTEXT_PROCESSORS =(
16      "django.contrib.auth.context_processors.auth",
17      "django.core.context_processors.debug",
18      "django.core.context_processors.i18n",
19      "django.core.context_processors.media",
20      "django.core.context_processors.static",
21      "django.core.context_processors.tz",
22      "django.contrib.messages.context_processors.messages",
23      "django.core.context_processors.request",
24  )
25  
26  TEMPLATE_DIRS = (
27      os.path.join(BASE_DIR, 'templates'),
28      os.path.join(BASE_DIR, '/loginsys/templates'),
29      os.path.join(BASE_DIR, '/table_view/templates'),
30  )
31  
32  # Quick-start development settings - unsuitable for production
33  # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
34  
35  # SECURITY WARNING: keep the secret key used in production secret!
36  SECRET_KEY = 'eszu&dq85t6zzd&1+e7a!5mbi*0em+%cev8_zntm3v5u6o+@zt'
37  
38  # SECURITY WARNING: don't run with debug turned on in production!
39  DEBUG = True
40  
41  TEMPLATE_DEBUG = True
42  
43  ALLOWED_HOSTS = []
44  
45  
46  # Application definition
47  
48  INSTALLED_APPS = (
49      'django.contrib.admin',
50      'django.contrib.auth',
51      'django.contrib.contenttypes',
52      'django.contrib.sessions',
53      'django.contrib.messages',
54      'django.contrib.staticfiles',
55      'table_view',
56     
57      'loginsys',
58  )
59  
60  MIDDLEWARE_CLASSES = (
61      'django.contrib.sessions.middleware.SessionMiddleware',
62      'django.middleware.common.CommonMiddleware',
63      'django.middleware.csrf.CsrfViewMiddleware',
64      'django.contrib.auth.middleware.AuthenticationMiddleware',
65      'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
66      'django.contrib.messages.middleware.MessageMiddleware',
67      'django.middleware.clickjacking.XFrameOptionsMiddleware',
68  )
69  
70  ROOT_URLCONF = 'table.urls'
71  
72  WSGI_APPLICATION = 'table.wsgi.application'
73  
74  
75  # Database
76  # https://docs.djangoproject.com/en/1.7/ref/settings/#databases
77  
78  DATABASES = {
79      'default': {
80          'ENGINE': 'django.db.backends.sqlite3',
81          'NAME': os.path.join(BASE_DIR, 'book_db.sqlite3'),
82      }
83  }
84  
85  # Internationalization
86  # https://docs.djangoproject.com/en/1.7/topics/i18n/
87  
88  LANGUAGE_CODE = 'en-us'
89  
90  TIME_ZONE = 'UTC'
91  
92  USE_I18N = True
93  
94  USE_L10N = True
95  
96  USE_TZ = True
97  
98  
99  # Static files (CSS, JavaScript, Images)
100 # https://docs.djangoproject.com/en/1.7/howto/static-files/
101 
102 STATIC_URL = '/static/'
103 
104 STATICFILES_DIRS = (
105     ('static', os.path.join(BASE_DIR, 'static')),
106 )

【问题讨论】:

  • 您不需要将'/loginsys/templates''/table_view/templates' 添加到TEMPLATE_DIRS,django 会在所有已安装的应用程序中搜索“模板”目录,并自动将它们添加到模板搜索路径中。

标签: python django apache


【解决方案1】:

运行

python manage.py collectstatic 

将静态文件收集到 STATIC_ROOT 中,然后由 apache 提供服务。

【讨论】:

    猜你喜欢
    • 2014-11-24
    • 1970-01-01
    • 2012-03-19
    • 2012-12-16
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    相关资源
    最近更新 更多