0

这是我的视图功能:

def detail(request, movie_id):
    movie = Movie.objects.get(id = movie_id)
    movieReleases = Release.objects.filter(movie = movie)
    reviews = Review.objects.filter(movie = movie)

    initRating = 0

    if request.user.is_authenticated():
        user = Account.objects.get(user = request.user.id)
        try:
            myreview = Review.objects.get(movie = movie, user = user)
            initRating = myreview.rating
        except:
            pass

        releaseContext = dict()
        for release in movieReleases:
            if release.isReleased():
                releaseContext[release.id] = {'buttonURL' : reverse('movies.views.add', args=(release.id,)), 'buttonText' : 'Add to cart'}
            elif MovieReleaseReminder.objects.filter(account=user, movie=release).exists():
                releaseContext[release.id] = {'buttonURL' : reverse('movies.views.unsubscribeFromReleaseReminder', args=(release.id,)), 'buttonText' : 'Unsubscribe from release reminder'}
            else:
                releaseContext[release.id] = {'buttonURL' : reverse('movies.views.subscribeForReleaseReminder', args=(release.id,)), 'buttonText' : 'Subscribe to release reminder'}

        if MovieUpdateSubscription.objects.filter(account=user, movie=movie).exists():
            movieSubscribeURL = reverse('movies.views.unsubscribeFromUpdates', args=(movie.id,))
            movieSubscribeButtonText = 'Unsubscribe from updates'
        else:
            movieSubscribeURL = reverse('movies.views.subscribeForUpdates', args=(movie.id,))
            movieSubscribeButtonText = 'Subscribe for updates'

    else:
        releaseContext = None
        movieSubscribeURL = None
        movieSubscribeButtonText = None


    averageRating = calculateAverageRating(movie)

    t = loader.get_template('movies/detail.html')
    c = RequestContext(request, {'movie' : movie,
                                 'movieReleases' : movieReleases,
                                 'releaseContext' : releaseContext,
                                 'reviews': reviews,
                                 'averageRating': averageRating,
                                 'initRating': initRating,
                                 'movieSubscribeURL' : movieSubscribeURL,
                                 'movieSubscribeButtonText' : movieSubscribeButtonText})
    return HttpResponse(t.render(c))

这是有问题的模板代码:

{% if movieReleases%}
    <table border="1px">
        {% for release in movieReleases %}
            <tr>
                <td>{{release.format}}</td>
                <td>{{release.region}}</td>
                <td>{{release.price}}</td>
                {% if user.is_authenticated %}
                    {% with release_id=release.id %}
                        <td><form action="{{ releaseContext.release_id.buttonURL }}" method="post">{% csrf_token %}<input type="submit" value="{{ releaseContext.release_id.buttonText }}" /></form></td>
                    {% endwith %}
                {% endif %}
            </tr>
        {% endfor %}
    </table>
{% endif %}

问题是,在渲染模板时,两者{{ releaseContext.release_id.buttonURL }}{{ releaseContext.release_id.buttonText }}评估为空字符串,即使我已经在调试器中检查了 releaseContext 具有适当的数据。我错过了什么?

4

2 回答 2

1

由于releaseContext是字典,因此您需要的不是属性查找 (xy),而是元素查找 (x[y])。

所以而不是:

{{ releaseContext.release_id.buttonURL }}

利用

{{ releaseContext['release_id']['buttonURL'] }}

于 2012-08-02T02:42:39.220 回答
0

在将值传递给模板之前尝试从字典中获取值:

{% display = releaseContext['release_id']['buttonURL'] %}
{{ display }}
于 2018-06-20T17:59:42.260 回答