简体   繁体   中英

serving static files django development

I know a million people have asked this, and I've read and read but still cannot get this to work (I think that says something about the documentation cough cough).

ONE. CSS: Here are my settings:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'))
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static")
STATIC_ROOT = '/static/'
STATIC_URL = '/static/'

also relevant:

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

My templates are serving up just fine, while my css is not. its located in static/originaltheme.css

I've tried both:

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

and

<link rel="stylesheet" type="text/css" href="/static/originaltheme.css">

TWO. Also, is there a way to get hardcoded urls to display? for example, lets say the content of a blog post has a hardcoded url to /static/img1.jpg, how can I do that? All of the documention points to {{ static_url }}, but im guessing that that wont get evaluated when returned from a text field of a database...

is there something to do with my urls?

from django.conf.urls import patterns, include, url

from django.contrib import admin
from django.conf.urls.static import static
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'nickswebsite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^$', 'blog.views.index'),

    url(r'^blog/view/(?P<slug>[^\.]+)',
    'blog.views.view_post',
    name='view_blog_post'),

    url(r'^blog/category/(?P<slug>[^\.]+)',
    'blog.views.view_category',
    name='view_blog_category'),

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

EDIT:

I tried doing this is in the urls:

urlpatterns = patterns('',
    url(r'^$', 'blog.views.index'),

    url(r'^blog/view/(?P<slug>[^\.]+)',
    'blog.views.view_post',
    name='view_blog_post'),

    url(r'^blog/category/(?P<slug>[^\.]+)',
    'blog.views.view_category',
    name='view_blog_category'),

    url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=setting.STATIC_ROOT)

urlpatterns += staticfiles_urlpatterns()

but im getting a "settings" undefined error. I tried to import settings and im still getting an error.


Edit

Okay, the documentation for serving static off development server is HORRIBLE. Whoever wrote it should really rewrite it. This guy is the only person in the entire world who seems to explain this clearly: http://agiliq.com/blog/2013/03/serving-static-files-in-django/#servee

You DO need these in the settings.py (why arent they included in the initial installaion??? what genius...):

STATICFILES_FINDERS = ( 
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
   'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

You do NOT need these: STATICFILES_DIRS = (os.path.join(BASE_DIR, "static") STATIC_ROOT = '/static/'

You do NOT need:

manage.py collectstatic

http://agiliq.com/blog/2013/03/serving-static-files-in-django/#servee :

Points to be noted

We did not make any changes to any of the static settings provided by Django to us. We left the static settings as they were in default settings.py provided by Django.

You don't need any change in your urls.py for serving your static files in development. You don't need to add staticfiles_urlpatterns(). Many a times, I got confused with this.

You do not need python manage.py collectstatic for serving static files in development.

I also fought against this particular problem. And finally came up with this blog post

Actually you don't need collectstatic , STATICFILES_FINDER and django.contrib.staticfiles . They are all related. Read the blog post

I added these lines to the bottom of my url.py :

if settings.DEBUG:
urlpatterns += patterns('',
                        url(r'^uploads/(?P<path>.*)$', 'django.views.static.serve', {
                            'document_root': settings.MEDIA_ROOT,
                        }),
)

And this in my settings.py :

STATICFILES_DIRS = ()

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

Did you use:

./manage.py collectstatic

Also your STATIC_ROOT should be absolute path to the directory static files should be collected to:

STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR, "static"))

This is what I did for Django 2.0 using the above answers. So you'll want a system that makes deployment easy.

settings.py:

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

# Serve Static in Development

STATICFILES_FINDERS = ( 
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_DIRS = [os.path.join(BASE_DIR, "appName/static/appName")]

# Serve static in production

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Then made a directory ~/documentRoot/appName/static/appName/ and copied my png into it. Then in my template:

<head>
  <bootstrap,jquery,etc...>
  {% load staticfiles %}
</head>
<body>
<img class="mb-4" src="{% static "myImage.png" %}" alt="" width="72" height="72">
</body>

Then when you want to deploy you just run collectstatic and they'll be served in one spot. Each time you add an app, add to that list of static dirs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM