您是否忘记在django中注册或加载此标记?

时间:2017-07-07 03:22:13

标签: javascript python html css django

我试图在wine_list .html中插入wine的图像,我得到了这个错误

第21行的块标记无效:'static'reviews \ images \ download(1).jpg'',预期'空'或'endfor'。您是否忘记注册或加载此标记?

以下是wine_list

的代码
{% extends 'base.html' %}

    {% block title %}
    <h2>Wine list</h2>
    {% endblock %}

    {% block content %}
    {% if wine_list %}
    {% load static %}

    <div>
        {% for wine in wine_list %}
        <div>
            <h4><a href="{% url 'reviews:wine_detail' wine.id %}">
            {{ wine.name }}
                   {
    <div class="w-col w-col-3 download">
              <a href="#"><img class="img-rounded" 
                src="{% static'reviews\images\download(1).jpg'%}" alt="CEe"></a>
            </div>
        }
            </a></h4>
            <h5>{{ wine.review_set.count }} reviews</h5>
            <h5> average rating</h5>
        </div>
        {% endfor %}
    </div>
    {% else %}
        <p>No wines are available.</p>
    {% endif %}
    {% endblock %}

这里也是views.py

的代码
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Review, Wine
from .forms import ReviewForm
import datetime

from django.contrib.auth.decorators import login_required


def review_list(request):
    latest_review_list = Review.objects.order_by('-pub_date')[:9]
    context = {'latest_review_list': latest_review_list}
    return render(request, 'reviews/review_list.html', context)


def review_detail(request, review_id):
    review = get_object_or_404(Review, pk=review_id)
    return render(request, 'reviews/review_detail.html', {'review': review})


def wine_list(request):
    wine_list = Wine.objects.order_by('-name')
    context = {'wine_list': wine_list}
    return render(request, 'reviews/wine_list.html', context)


def wine_detail(request, wine_id):
    wine = get_object_or_404(Wine, pk=wine_id)
    form = ReviewForm()
    return render(request, 'reviews/wine_detail.html', {'wine': wine, 'form': form})


@login_required
def add_review(request, wine_id):
    wine = get_object_or_404(Wine, pk=wine_id)
    form = ReviewForm(request.POST)
    if form.is_valid():
        rating = form.cleaned_data['rating']
        comment = form.cleaned_data['comment']
        user_name = request.user.username
        review = Review()
        review.wine = wine
        review.user_name = user_name
        review.rating = rating
        review.comment = comment
        review.pub_date = datetime.datetime.now()
        review.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('reviews:wine_detail', args=(wine.id,)))

    return render(request, 'reviews/wine_detail.html', {'wine': wine, 'form': form})



def user_review_list(request, username=None):
    if not username:
        username = request.user.username
    latest_review_list = Review.objects.filter(user_name=username).order_by('-pub_date')
    context = {'latest_review_list':latest_review_list, 'username':username}
    return render(request, 'reviews/user_review_list.html', context)


@login_required
def user_recommendation_list(request):
    # get this user reviews
    user_reviews = Review.objects.filter(user_name=request.user.username).prefetch_related('wine')
    # from the reviews, get a set of wine IDs
    user_reviews_wine_ids = set(map(lambda x: x.wine.id, user_reviews))
    # then get a wine list excluding the previous IDs
    wine_list = Wine.objects.exclude(id__in=user_reviews_wine_ids)

    return render(
        request,
        'reviews/user_recommendation_list.html',
        {'username': request.user.username,'wine_list': wine_list}
    )

1 个答案:

答案 0 :(得分:2)

您的静态标签上缺少空格,应为

{% static 'reviews\images\download(1).jpg' %}

此外,通常最好在扩展后立即将加载标记保持在顶部。

如果未加载图像,请检查图像是否通过直接在浏览器中显示路径显示,如果未显示,则可能是STATIC_URL和STATIC_ROOT设置存在问题。