Django url不';看不到上下文变量
我在纵断面图中设置了上下文变量-用户尼克:Django url不';看不到上下文变量,django,django-templates,django-urls,Django,Django Templates,Django Urls,我在纵断面图中设置了上下文变量-用户尼克: class ProfileView(LoginRequiredMixin, TemplateView): template_name = 'profile/templates/profile.html' redirect_field_name = '' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) p
class ProfileView(LoginRequiredMixin, TemplateView):
template_name = 'profile/templates/profile.html'
redirect_field_name = ''
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
profile_id = Profile.objects.get(pk=self.request.user.id)
context['user_nick'] = Blogger.objects.get(profile_id=profile_id).nick
return context
和它的url:
path('bloggers/<slug:nick>/videos', bloggers_views.BloggerVideos.as_view(), name='blogger_videos'),
对它的引用如下:
<li><a href= {% url 'bloggers' %}><span>Bloggers {{ nick }}</span></a></li>
<li><a href= {% url 'blogger_videos' nick %}>Blogger's videos</a></li>
可能的解决方案是什么?您正在以
用户nick
的身份传递变量,而不是nick
。另外,您应该在TagBy关键字参数中引用它。第三,你应该把整个URL放在HTML的引号里
<a href="{% url 'blogger_videos' nick=user_nick %}">
看起来博客作者没有尼克。你确定它有外号吗?你能打印(…)
内容吗?也许你不应该用nick=user\u nick
打印,而应该用nick=user.nick
?@WillemVanOnsem不,它有。@ozgur不,user\u nick在配置文件页面中呈现为ok。问题中的代码是否反映了你的实际缩进?如果是这样,get\u context\u data
将永远不会被调用,那么它需要缩进ProfileView类中。Omg,对不起。在我的代码中,它完全缩进到ProfileView类中
NoReverseMatch at /bloggers/Gardiner/videos
Reverse for 'blogger_videos' with arguments '('',)' not found. 1 pattern(s)
tried: ['bloggers\\/(?P<nick>[-a-zA-Z0-9_]+)\\/videos$']
candidate_subs {'nick': ''}
<a href="{% url 'blogger_videos' nick=user_nick %}">