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 に適切なデータがあることをデバッガーで確認したにもかかわらず、テンプレートがレンダリングされるときに と の両方が空の文字列に評価されることです{{ releaseContext.release_id.buttonURL }}{{ releaseContext.release_id.buttonText }}私は何が欠けていますか?

4

2 に答える 2

1

は辞書であるためreleaseContext、属性ルックアップ(xy)は必要なものではなく、要素ルックアップ(x [y])です。

したがって、代わりに:

{{ releaseContext.release_id.buttonURL }}

使用する

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

于 2012-08-02T02:42:39.220 に答える