1
非常簡單的問題需要一個非常簡單的解決方案。我需要在稱爲profile_detail的配置文件視圖中添加額外的上下文,同時仍包含原始上下文。這是userena視圖。將上下文添加到django userena視圖
def profile_detail(request, username,
template_name=userena_settings.USERENA_PROFILE_DETAIL_TEMPLATE,
extra_context=None, **kwargs):
.........................
.........................
user = get_object_or_404(User,
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
return ExtraContextTemplateView.as_view(template_name=template_name,
extra_context=extra_context)(request)
我被告知這可行。首先,我將用戶視圖導入爲userena_views
。然後我嘗試創建我的上下文,然後使用userena視圖發送請求,並更改我的url以指向此視圖。
def profileview(request,username):
user=User.objects.get(username=username)
usergigs=Gig.objects.filter(user.id)
extra_context['usergig']=usergigs
return userena_views.profile_detail(request)
這沒有奏效,這是正確的方法嗎?有沒有一種優雅的方式來做到這一點?或者,我是唯一選擇將視圖複製到我的視圖並從那裏編輯?