我正在尝试将以下参数传递给django中的render to response语法,但我不断收到以下错误:
render_to_string()获得了参数' context_instance'的多个值。
尝试了不同的解决方案,但仍然没有做到正确。有什么帮助吗?
views.py:
@login_required
def articles(request, everyone=True):
events = post.objects.filter()
if request.user.is_authenticated():
my_events = events.filter(user=request.user)
events = events.exclude(user=request.user)
following = request.user.following_set.all().values_list('to_user',
flat=True)
else:
my_events = post.objects.none()
following = None
if not everyone:
events = events.filter(user__in=following)
args = {}
args.update(csrf(request))
args ['posts'] = post.objects.filter(user = request.user)
args ['full_name'] = User.objects.get(username = request.user.username)
args ['form'] = PostForm()
args ['forms'] = CommentForm()
args ['profile_picture'] = request.user.profile.profile_picture
context = {
'events': events,
'my_events': my_events,
'following': following,
}
return render_to_response('articles.html', args, context,
context_instance = RequestContext(request))
有什么问题吗?
答案 0 :(得分:0)
这是来自django source django.template.engine的render_to_string:
def render_to_string(self, template_name, dictionary=None, context_instance=None,
dirs=_dirs_undefined):
从(*args,**kwargs)
传递render_to_response()
来调用它很明显,您的context
参数与您分配的关键字参数context_instance
冲突。你应该只发送其中一个。也许您应该将context
中的数据放在args
词典中?
顺便说一下,你可以在这里查看文档: https://docs.djangoproject.com/en/dev/ref/templates/api/#the-render-to-string-shortcut 和这里: https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to-response
查看每个函数接受的参数列表。