Django模板在子路由上获取错误的静态文件URL

时间:2014-11-07 02:53:47

标签: python html django

简单的问题,除了在这里粘贴我的代码之外,还不确定如何解释它....图像正在索引视图上加载,而不是category_photos视图。

以下是完整说明这一点的HTTP响应:

"GET / HTTP/1.1" 200 16912
"GET /static/media/uploads/CF-10.jpg HTTP/1.1" 304 0
"GET /static/media/uploads/CF-08.jpg HTTP/1.1" 304 0
"GET /static/media/uploads/CF-09.jpg HTTP/1.1" 304 0
"GET /group/2/ HTTP/1.1" 200 16194
"GET /group/2/static/media/uploads/CF-09.jpg HTTP/1.1" 404 13515
"GET /group/2/static/media/uploads/CF-08.jpg HTTP/1.1" 404 13515
"GET /group/2/static/media/uploads/CF-10.jpg HTTP/1.1" 404 13515

urls.py

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

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

    url(r'group/(?P<pk>\d+)/$',
        'portfolio.views.category_photos',
        name="category_photos"),
)

views.py

def index(request):
    groups = Group.objects.all()
    context = {'groups': groups}
    return render(request, 'portfolio/index.html', context)

def category_photos(request, pk):
    groups = Group.objects.all()
    selected = Group.objects.filter(id=pk)[0]

    return render(request, 'portfolio/individual-group.html', {'groups': groups, 'selected': selected})

相关模板:

index.html(这一项工作)

{% for group in groups %}
    <h2 class="clearfix:before" id="{{group.abbv}}">{{group.title}}</h1>
    <div id="gallery-{{group.id}}" class="gallery container">

        {% for photo in group.photos.all %}
        <div class="box">
            <a href="/{{ photo.image.url }}" title="{{ photo.category_image }}">
                <img src="/{{photo.image.url}}" alt="{{photo.category_image}}">
            </a>
        </div>
        {% endfor %}
    </div>
{% endfor %}

individual-group.html(&gt;&gt;&gt;&gt;这一步&#39;工作&lt;&lt;&lt;&lt;&lt;&lt;)

{% for photo in selected.photos.all %}
<div class="box">
    <a href="/{{photo.image.url }}" title="{{ photo.category_image }}">
        <img src="/{{photo.image.url}}" alt="{{photo.category_image}}">
    </a>
</div>
{% endfor %}

Settings.py

仅包含媒体/静态内容,因为我非常确定这是一个什么样的问题

PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..', '..')

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'static/media')
MEDIA_URL = 'static/media/uploads/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'

1 个答案:

答案 0 :(得分:0)

好的,所以我愿意接受其他可以解释这个问题的答案,但这解决了这个问题 - 请告诉我这是不是很愚蠢。

urls.py

from django.conf import settings

...是......小时......小时候想出来。我会哭着自己睡觉。