每次编辑静态文件时都需要manage.py collectstatic吗?

时间:2022-04-29 23:19:16

I was using django- 1.3.1 by now. Want to give a try to latest version i.e 1.8.1. So, configured a test project. I was unfamiliar how to tackle with static files with django-1.8.1, since explored it more.

我现在正在使用django-1.3.1。想尝试最新版本,即1.8.1。所以,配置了一个测试项目。我不熟悉如何使用django-1.8.1处理静态文件,因为我对它进行了更多的探索。

I created a /var/www/appmedia/ directory and put my static components in to it. like bootstrap, css, js, images, etc.

我创建了一个/ var / www / appmedia /目录并将我的静态组件放入其中。像bootstrap,css,js,images等

settings.py.

settings.py。

"""
Django settings for myapp project.

Generated by 'django-admin startproject' using Django 1.8.1...
"""

# 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 = '+3)e5#343!^(cvy8f9c4hpku*h#$z8*sa)(7a#azv1!!z@d--wsq5s5'

# 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',
    'myapp.common'
)

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 = 'myapp.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ["/home/trex/Work/myapp/myapp/templates"],
        '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 = 'myapp.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': "myapp",
        'USER':"postgres", 
        'PASSWORD':"postgres", 
        'HOST':"" 
    }
}


# 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_ROOT = '/home/trex/Work/myapp/myapp/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    '/var/www/appmedia/',
)

To collect static I run python manage.py collectstatic which created following

为了收集静态,我运行python manage.py collectstatic,它创建了以下内容

directory structure:

目录结构:

├── myapp
│   ├── common
│   │   ├── migrations
│   │   ├── __pycache__
│   │   └── static
│   ├── __pycache__
│   ├── static
│   │   ├── admin
│   │   ├── bootstrap
│   │   ├── css
│   │   ├── images
│   │   ├── js
│   │   └── scripts
│   └── templates
└── manage.py

urls.py

urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', views.home, name='home'),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

The issue is, I'm unable to edit my static files. Always have to keep it in /var/www/appmedia to edit it, run the collectstatic, then only my changes to static files are reflecting. Did I miss something in configuring static files?

问题是,我无法编辑我的静态文件。总是必须将它保存在/ var / www / appmedia中进行编辑,运行collectstatic,然后只有我对静态文件的更改才会反映出来。我是否遗漏了配置静态文件的内容?

3 个解决方案

#1


7  

That is because you are pointing your server to

那是因为你指的是你的服务器

STATIC_ROOT = '/home/trex/Work/myapp/myapp/static'.

When developing and using runserver, running collectstatic is not needed, nor does STATIC_ROOT need to be set. You can simply use the files from your "static" folder that you define at the app level. STATICFILES_DIRS is used only when you have static files that you are using from a different location than a "static" directory in your apps.

在开发和使用runserver时,不需要运行collectstatic,也不需要设置STATIC_ROOT。您只需使用您在应用级别定义的“静态”文件夹中的文件即可。 STATICFILES_DIRS仅在您使用的静态文件位于应用程序中的“静态”目录以外的其他位置时使用。

Check out the documentation on this with attention to the different ways you serve your files: development vs. production

查看相关文档,注意文件服务的不同方式:开发与生产

editing for code addition to urlconf

from django.conf import settings 
from django.contrib.staticfiles.views import serve 
if settings.DEBUG: 
    urlpatterns += [ url(r'^static/(?P<path>.*)$', serve), ] 

#2


1  

For the development server of Django, you can add the following in your settings file:

对于Django的开发服务器,您可以在设置文件中添加以下内容:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    'static/',
)

This should be good enough and you wouldn't need to run collectstatic each time.

这应该足够好,你不需要每次都运行collectstatic。

For your live server settings remove this line or set it to None and make sure to add STATIC_ROOT.

对于您的实时服务器设置,请删除此行或将其设置为“无”,并确保添加STATIC_ROOT。

#3


1  

I just went through the same issue and after spending hours on it I finally found the easiest break through. Chaos's method didn't work for me so I dug a little deeper and changed static settings to following:

我刚刚经历了同样的问题,花了几个小时后,我终于找到了最简单的突破。 Chaos的方法对我没有用,所以我挖得更深一些,并将静态设置更改为以下内容:

STATIC_URL = '/static/'

if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static','static-only')
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static','media')
STATICFILES_DIRS = (os.path.join(os.path.dirname(BASE_DIR),'static'),)

After this I had changed my urls.py file which now has the following addition

在此之后我改变了我的urls.py文件,该文件现在增加了以下内容

if settings.DEBUG:
   urlpatterns += static(settings.STATIC_URL,document_root = settings.STATIC_ROOT)
   urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)

Needless to say, you have to import settings into your urls.py file

不用说,您必须将设置导入到urls.py文件中

#1


7  

That is because you are pointing your server to

那是因为你指的是你的服务器

STATIC_ROOT = '/home/trex/Work/myapp/myapp/static'.

When developing and using runserver, running collectstatic is not needed, nor does STATIC_ROOT need to be set. You can simply use the files from your "static" folder that you define at the app level. STATICFILES_DIRS is used only when you have static files that you are using from a different location than a "static" directory in your apps.

在开发和使用runserver时,不需要运行collectstatic,也不需要设置STATIC_ROOT。您只需使用您在应用级别定义的“静态”文件夹中的文件即可。 STATICFILES_DIRS仅在您使用的静态文件位于应用程序中的“静态”目录以外的其他位置时使用。

Check out the documentation on this with attention to the different ways you serve your files: development vs. production

查看相关文档,注意文件服务的不同方式:开发与生产

editing for code addition to urlconf

from django.conf import settings 
from django.contrib.staticfiles.views import serve 
if settings.DEBUG: 
    urlpatterns += [ url(r'^static/(?P<path>.*)$', serve), ] 

#2


1  

For the development server of Django, you can add the following in your settings file:

对于Django的开发服务器,您可以在设置文件中添加以下内容:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    'static/',
)

This should be good enough and you wouldn't need to run collectstatic each time.

这应该足够好,你不需要每次都运行collectstatic。

For your live server settings remove this line or set it to None and make sure to add STATIC_ROOT.

对于您的实时服务器设置,请删除此行或将其设置为“无”,并确保添加STATIC_ROOT。

#3


1  

I just went through the same issue and after spending hours on it I finally found the easiest break through. Chaos's method didn't work for me so I dug a little deeper and changed static settings to following:

我刚刚经历了同样的问题,花了几个小时后,我终于找到了最简单的突破。 Chaos的方法对我没有用,所以我挖得更深一些,并将静态设置更改为以下内容:

STATIC_URL = '/static/'

if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static','static-only')
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static','media')
STATICFILES_DIRS = (os.path.join(os.path.dirname(BASE_DIR),'static'),)

After this I had changed my urls.py file which now has the following addition

在此之后我改变了我的urls.py文件,该文件现在增加了以下内容

if settings.DEBUG:
   urlpatterns += static(settings.STATIC_URL,document_root = settings.STATIC_ROOT)
   urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)

Needless to say, you have to import settings into your urls.py file

不用说,您必须将设置导入到urls.py文件中