1

我已经按照此处的说明完成了本教程:

这里

但有些地方我无法理解。

当我使用 Facebook 帐户登录时,我也可以轻松访问我的管理页面并且我希望它发生(因为它不安全),那么有没有办法解决这个问题?

如果我想将该注册用户带到另一个模板,我只能在我的 url 调度程序中使用 direct_to_template 方法,这是一个示例: url(r'^tags$', direct_to_template, {'template' : 'user.html' })

还有另一种方法吗?

最后为了更清楚,这里是我的项目的一些片段:

网址.py

urlpatterns = patterns('',
#All Auth URLS
url(r'^accounts/', include('allauth.urls')),
url(r'^accounts/profile/', direct_to_template, { 'template' : 'profile.html' }),
#nav urls
url(r'^$','fb.views.home', name="home"),
url(r'^tags$', direct_to_template, {'template' : 'tags.html' }),

视图.py

def home(request):
return render_to_response("base.html", locals(), RequestContext(Context))

base.html

......

{% block body %}
{% block content %}

{% if user.is_authenticated %}
    {{ user.username }}  <p> you are logged in </p>
    <p><a href="/accounts/logout/" >Logout </a></p>
  {% else %}    
    <p> you are not authenticated :  </p>
    <a href="/accounts/facebook/login/" >Login with Facebook </a>
  {% endif %}
{% endblock content %}
{% endblock body %}

...

profile.html

...
{% block content %}

{% if user %}

<h1>Welcome, {{user.first_name}}</h1>
<p>Following is the Extra information that facebook has provided to allauth:</p>
{% for account in user.socialaccount_set.all %}
    <p>First Name: {{ account.extra_data.first_name }}</p>
    <p>Last Name: {{ account.extra_data.last_name }}</p>
    <p>Profile Link: <a href="{{ account.extra_data.link }}">{{ account.extra_data.link   }}</a></p>
{% endfor %}
{% endif %}

<a href="/tags">  Go to tags</a>

{% endblock content %}
{% endblock body %}

标签.html

{{user.socialaccount_set.all.0.get_avatar_url}} <br/>
{{user.socialaccount_set.all.0.uid}} <br/>
{{user.socialaccount_set.all.0.get_provider_account }} <br/>

最后提前感谢您的帮助。

4

1 回答 1

0

秘诀是:

登录后,用户隐藏在请求对象中,所以视图应该是这样的:

def home(request):
    user = request.user
    return render_to_response("base.html", locals(), RequestContext(Context))

现在,问题解决了,但我想知道为什么 Context User 对象是匿名的。

我问这个问题是因为最后一个用户对象在简单的身份验证中将具有与请求用户相同的值

于 2013-01-08T14:12:15.740 回答