gpt4 book ai didi

django - 服务静态文件 Django 开发

转载 作者:行者123 更新时间:2023-12-04 15:34:29 24 4
gpt4 key购买 nike

我知道有一百万人问过这个问题,我读了又读,但仍然无法让它工作(我认为这说明了文档咳咳)。

一。 CSS:
这是我的设置:

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/'

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

我的模板可以正常运行,而我的 css 则不能。它位于 static/originaltheme.css

我都试过:
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}originaltheme.css">


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

二。另外,有没有办法让硬编码的网址显示出来?例如,假设博客文章的内容有一个硬编码的 url 到/static/img1.jpg,我该怎么做?所有文档都指向 {{ static_url }},但我猜测从数据库的文本字段返回时不会对其进行评估...

与我的网址有关吗?
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)),
)

编辑:

我尝试在网址中执行此操作:
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()

但我收到“设置”未定义错误。我尝试导入设置,但仍然出现错误。

编辑

好的,为静态关闭开发服务器提供服务的文档是可怕的。写它的人真的应该重写它。这个人似乎是全世界唯一一个解释清楚的人: http://agiliq.com/blog/2013/03/serving-static-files-in-django/#servee

你确实需要在 settings.py 中使用这些(为什么它们不包含在初始安装中???什么天才......):
STATICFILES_FINDERS = ( 
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

你不需要这些:
STATICFILES_DIRS = (os.path.join(BASE_DIR,“静态”)
STATIC_ROOT = '/静态/'

你不需要:
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.

最佳答案

我也与这个特殊问题作斗争。最后想出了this blog post

其实你不需要collectstatic , STATICFILES_FINDERdjango.contrib.staticfiles .它们都是相关的。阅读 blog post

关于django - 服务静态文件 Django 开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21765741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com