2017-04-30 57 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>

相关问题