2017-04-30 60 views
0

我正在使用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?

回答

0

我只是快速瀏覽了由allauth應用提供的模板標籤。我沒有測試過這個(也沒有使用這個項目),但是這看起來像你正在尋找的東西。

在你的模板,任何模板...

{% load account %} {# this should be near the top with any other load tags #} 

{# this can be anywhere in the template. I'm not positive what it returns but it looks like it is the username. #} 
<p> 
    {% user_display user %} 
</p> 

我找到了答案here

+0

沒了!那不是我正在尋找的..有沒有其他方法可以做到這一點?可能從零開始寫東西 – Rahul

+0

嘿@Rahul,我不確定django-allauth在做什麼,但通常用戶信息通過'{{user}}或'{{request.user}}提供給每個視圖' 。做那兩項工作?如果沒有,那麼django-allauth正在爲中間件做些什麼。在這種情況下,您可以編寫自定義模板標籤以從allauth獲取用戶名。看到這裏:https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/ – teewuane

+0

如果你想與一個手,讓我知道。 – teewuane

0

我得到它的工作,謝謝@teewuane

sidebar.html

<a href="{% url 'profile_view' slug=profile.username %}">Profile View</a>

更換爲

<a href="{% url 'profile_view' slug=request.user.username %}">Profile View</a>

相關問題