0
簡單的問題,不知道如何解釋它除了粘貼我的代碼在這裏....圖像加載索引視圖,而不是category_photos視圖。Django模板在子路徑上獲取錯誤的靜態文件url
下面是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 %}
個體group.html(>>>> THIS ONE不起作用< < < <)
{% 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
只包括媒體/靜態的東西,因爲我很確定這是什麼第eproblem是
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/'
爲什麼你有'./'?理想情況下,你要做'{{STATIC_URL}} {{{{photo.image.url}}}}''。/'正在尋找相對於當前工作目錄的路徑,而'/'正在尋找項目根目錄,這是當前目錄上面的2個目錄 – karthikr 2014-11-07 03:31:02
哦,我正在試驗,可能不在那裏了。讓我再看一遍。 – 2014-11-07 17:53:02
好吧,重新確認有一個主要的斜槓 - 我同意似乎並不理想,並導致我相信某些東西仍然時髦w /我的配置。 – 2014-11-07 17:54:55