1

在阅读了有关常见 Django 项目结构的更多信息后,我将我的“媒体”文件夹重命名为“静态”,因为它包含我的 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?不太确定我在想什么......无论如何,我已经根据文档对我的代码进行了我认为的适当更改,但我仍然无法让它工作。

设置.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/'

网址.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当我浏览代码时似乎可以正确解决(“静态”文件夹与 settings.py 位于同一目录中)。


编辑 2


根据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()

仍然没有运气。

4

1 回答 1

0

请参阅此处: 模板中的静态文件和 在开发中提供静态文件

于 2012-07-23T21:01:54.413 回答