在阅读了有关常见Django项目结构的更多信息之后,我将'media'文件夹重命名为'static',因为它包含我的web应用程序的CSS,javascript和图像,而且没有用户上传的文件。但是,我的项目没有找到新目录,并且仍在提供favicon,可以从localhost:8000 / media /images/favicon.png的旧目录下载!
在我的模板中,我直接链接到css / js文件,如下所示:
<link href="/static/css/map.css" type="text/css" rel="stylesheet" />
urls.py :
中也没有任何有趣的内容urlpatterns = patterns('',
url(r'^$', 'EventMapperApp.views.map', name='map'),
# AJAX
url(r'^all_events/', 'EventMapperApp.views.all_events'),
url(r'^user_created_events/', 'EventMapperApp.views.user_created_events'),
url(r'^all_user_events/', 'EventMapperApp.views.all_user_events'),
# event actions
url(r'^save_event/', 'EventMapperApp.views.save_event'),
url(r'^unsave_event/', 'EventMapperApp.views.unsave_event'),
)
有什么想法吗?
出于某种原因,我认为urls.py不必解析我的Django应用程序的所有URL?不确定我在想什么......无论如何,我根据文档做了我认为对我的代码进行适当修改的内容,但是我仍然遇到一些麻烦。
settings.py
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = (os.path.join(PROJECT_PATH, "media"))
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = (os.path.join(PROJECT_PATH, "static"))
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
urls.py
urlpatterns = patterns('',
# Examples:
url(r'^$', 'EventMapperApp.views.map', name='map'),
# Required to make static serving work
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
# AJAX
url(r'^all_events/', 'EventMapperApp.views.all_events'),
url(r'^user_created_events/', 'EventMapperApp.views.user_created_events'),
url(r'^all_user_events/', 'EventMapperApp.views.all_user_events'),
# event actions
url(r'^save_event/', 'EventMapperApp.views.save_event'),
url(r'^unsave_event/', 'EventMapperApp.views.unsave_event'),
)
模板
<link rel="icon"
type="image/png"
href="{{ STATIC_URL }}images/WWWfavicon.png" />
<link href="{{ STATIC_URL }}css/map.css" type="text/css" rel="stylesheet" />
<link href="{{ STATIC_URL }}css/ui-lightness/jquery-ui-1.8.21.custom.css" type="text/css" rel="stylesheet" />
<link href="{{ STATIC_URL }}css/timepicker.css" type="text/css" rel="stylesheet" />
我不确定我现在做错了什么... STATIC_ROOT
似乎在我浏览代码时正确解析('static'文件夹与settings.py位于同一目录中) 。
根据https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development,我将 urls.py 更改为以下内容:
from django.conf.urls import patterns, url
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
# Examples:
url(r'^$', 'EventMapperApp.views.map', name='map'),
# AJAX
url(r'^all_events/', 'EventMapperApp.views.all_events'),
url(r'^user_created_events/', 'EventMapperApp.views.user_created_events'),
url(r'^all_user_events/', 'EventMapperApp.views.all_user_events'),
# event actions
url(r'^save_event/', 'EventMapperApp.views.save_event'),
url(r'^unsave_event/', 'EventMapperApp.views.unsave_event'),
)
# only in development
urlpatterns += staticfiles_urlpatterns()
仍然没有运气。