向django userena视图添加上下文 [英] Add context to django userena view
问题描述
一个非常简单的问题需要一个非常简单的解决方案.我需要在名为profile_detail的配置文件视图中添加额外的上下文,同时仍要包含原始上下文.这是userena视图.
Very simple question need a very simple solution. I need to add extra context to a profile view called profile_detail while still including the original context. Here is the userena view.
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视图导入为 userena_views
.然后,我尝试创建上下文,然后使用userena视图发送请求,并更改我的URL指向该视图.
I was told this would work. First I imported the userena view as userena_views
. Then I tried to create my context, then send the request using the userena view and also change my urls to point to this view.
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)
这行不通,这是正确的方法吗?有一种优雅的方法吗?还是我唯一的选择是将视图复制到我的视图中并在其中进行编辑?
This didn't work, is this the right way? Is there an elegant way to do it? Or is my only option to copy the view into my views and edit from there?
推荐答案
可以这样编辑代码吗?
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, extra_context=extra_context)
这篇关于向django userena视图添加上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!