1

これは、アプリ (Userena) ビューに追加のクエリセット (リクエスト付き) を含める方法に関する私の他の質問の続きです。@limelights が提案したことを実行すると、コードは次のようになります。

見る:

from django.views.generic import list_detail

def requestuserswampers(request):
     qs = Thing.objects.filter(user=request.user)
     return list_detail.object_list(
                 request,
                 queryset = Thing.objects.all(),
                 template_object_name = 'thing',
                 extra_context = {'swamp_things': qs},
     )

URL:

url(r'^accounts/(?P<username>(?!signout|signup|signin)[\.\w-]+)/$',
       requestuserswampers,
       name='userena_profile_detail'),

これによりエラーが生成されTemplateDoesNotExistます: Template does not exist at myapp/swamp_things.html

一方、を使用してテンプレートの名前と場所を含めようとするとtemplate_name = 'userena/profile_detail.html'、適切なテンプレートがレンダリングされますが、デフォルトのユーザー名「profile_detail」テンプレートで通常レンダリングされるユーザー情報のように、コンテキストの一部が欠落しています..

ログインしているユーザーに基づいてオブジェクトをフィルタリングできるように、リクエストを許可する追加のクエリセットを Userena プロファイルの詳細ビューに追加するにはどうすればよいですか? アイデアをありがとう!

4

1 に答える 1

0

DRY の原則に反すると考えて、別のアプリのビューを書き直すことが慣行として受け入れられていることに気づきませんでした。しかし、必要なことを達成するための別の方法を見つけられず、上記のコメントで別の上級ユーザーによって承認されていたので、先に進み、Userena ビューを書き直そうとしました。私のクエリセットをに追加する必要がありましたextra_context

def profile_detail(request, username,
    template_name=userena_settings.USERENA_PROFILE_DETAIL_TEMPLATE,
    extra_context=None, **kwargs):

    user = get_object_or_404(get_user_model(),
                         username__iexact=username)

    profile_model = get_profile_model()
    try:
        profile = user.get_profile()
    except profile_model.DoesNotExist:
        profile = profile_model.objects.create(user=user)

    if not profile.can_view_profile(request.user):
        return HttpResponseForbidden(_("You don't have permission to view this profile."))
    if not extra_context: extra_context = dict()
    extra_context['profile'] = user.get_profile()
    extra_context['hide_email'] = userena_settings.USERENA_HIDE_EMAIL

        #### Added the line below
    extra_context['swamp_things'] = Thing.objects.filter(user=user) 

    return ExtraContextTemplateView.as_view(template_name=template_name,
                                            extra_context=extra_context)(request)
于 2013-07-17T19:01:39.230 に答える