我正在使用django-allauth
進行身份驗證。我有一個有很多鏈接的儀表板。Django將數據從一個應用程序傳遞給另一個應用程序?
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
觀點,我收到以下錯誤
反向與關鍵字「profile_view」參數'{'slug':''}'找不到。 1個圖案(多個)嘗試:[ '(?P [ - \瓦特@ + - ] +)/ $']
誤差不在ProfileView
顯示因爲profile.username
返回一個值,但是當我導航到change_password
視圖我得到上述錯誤,因爲profile
上下文對象只在該特定視圖中傳遞。我想通過該項目使用該context_object。
有沒有簡單的方法來做到這一點?除了構建JSON API?
沒了!那不是我正在尋找的..有沒有其他方法可以做到這一點?可能從零開始寫東西 – Rahul
嘿@Rahul,我不確定django-allauth在做什麼,但通常用戶信息通過'{{user}}或'{{request.user}}提供給每個視圖' 。做那兩項工作?如果沒有,那麼django-allauth正在爲中間件做些什麼。在這種情況下,您可以編寫自定義模板標籤以從allauth獲取用戶名。看到這裏:https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/ – teewuane
如果你想與一個手,讓我知道。 – teewuane