【发布时间】:2017-04-30 15:15:56
【问题描述】:
我使用django-allauth 进行身份验证。我有一个包含许多链接的仪表板。
user.html
`{% include 'sidebar.html' %}
<h1>Profile View</h1>
<p>{{ profile.username }}</p>`
change_password.html
`{% include 'sidebar.html' %}
<h2>Change Password</h2>
<p>{{ profile.username }}</p>`
sidebar.html
`<a href="{% url 'profile_view' slug=profile.username %}">Profile View</a>
<a href="{% url 'change_password' %}">Change Password</a>`
views.py
class ProfileView(DetailView):
template_name = "user.html"
queryset = User.objects.all()
context_object_name = 'profile'
slug_field = "username"
更改密码视图来自 django-allauth。如何将ProfileView 中的用户名传递到更改密码视图,以便我可以显示在change_password.html 页面中。
主要问题
由于我在两个视图中都包含sidebar.html,因此它在ProfileView 中运行良好,但是当我导航到change_password 视图时,我收到以下错误
未找到带有关键字参数“{'slug': ''}' 的“profile_view”的反向操作。尝试了 1 种模式:['(?P[-\w.@+-]+)/$']
错误未显示在ProfileView 中,因为profile.username 返回一个值,但是当我导航到change_password 视图时,我收到上述错误,因为profile 上下文对象仅在该特定视图中传递。我想在整个项目中使用那个 context_object。
有简单的方法吗?除了构建 JSON API?
【问题讨论】:
标签: python django django-templates