Django静态文件不在DetailView中提供

时间:2017-06-12 20:46:22

标签: python django django-staticfiles

尝试在我刚刚开始的应用中创建个人资料视图。我似乎无法弄清楚我遇到的这个简单问题。当我加载我的索引页面或任何其他没有pk的页面时,一切都正确加载。但是,每当我尝试在url中加载类似/ profile / 1 /的页面时,静态文件都不会加载。我一直得到404.请帮忙!我的代码已经很长了,但我会把它分解。我将添加我的设置,网址和视图以及个人资料页面。我想不出此时需要的任何其他东西。

Settings.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'dispatch/templates')
STATIC_DIR = os.path.join(BASE_DIR, 'dispatch/static')
MEDIA_DIR = os.path.join(BASE_DIR, 'dispatch/media')

STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR]

MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'

项目级别的URLS.py

from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'', include('dispatch.urls', namespace='dispatch')),
    url(r'^admin/', admin.site.urls),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

应用级别的网址:

# SET THE NAMESPACE!
app_name = 'dispatch'

# Be careful setting the name to just /login use userlogin instead!
urlpatterns=[
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^profile/(?P<pk>\d+)/$', views.ProfileDetailView.as_view(), name='profile_detail'),
]

Views.py

class ProfileDetailView(DetailView):
    context_object_name = 'profile_detail'
    model = models.UserProfile
    template_name = 'pages/profile.html'

信息页/ profile.html

{% extends 'lib/base.html' %}
{% load staticfiles %}

{% block page_css %}
    <link rel="stylesheet" type="text/css" href="{% static 'vendors/css/extensions/pace.css' %}">
    <link rel="stylesheet" type="text/css" href="{% static 'css/pages/users.css' %}">
{% endblock %}

{% block content %}
 <div align="right">

    <p>{{ profile_detail.user }}</p>
    <p>{{ profile_detail.zipcode }}</p>
    <img src="{{ profile_detail.profile_image }}" alt="Profile Image">

 </div>
{% endblock %}

来自manage.py的错误请注意路径中的/ profile / 1 /

[12/Jun/2017 20:40:04] "GET /profile/1/static/css/core/menu/menu-types/vertical-menu.css HTTP/1.1" 404 2570
[12/Jun/2017 20:40:04] "GET /media/CACHE/images/profile_images/20170612_110531/7d87ed0f89df89bff1d122fbf82bb83d.jpg HTTP/1.1" 304 0
Not Found: /profile/1/profile_images/20170612_110531.jpg
[12/Jun/2017 20:40:04] "GET /profile/1/profile_images/20170612_110531.jpg HTTP/1.1" 404 2525

0 个答案:

没有答案