python - Django:Twitter 登录被重定向到示例域

标签 python django python-2.7 twitter

我正在使用 django-socialregistration 使用户能够在我的应用程序上使用他们的 Twitter 帐户登录。

设置.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rango',
    'django.contrib.sites',
    'socialregistration',
    'socialregistration.contrib.twitter'
)
...
...
TEMPLATE_CONTEXT_PROCESSORS = ('django.contrib.auth.context_processors.auth', 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'django.core.context_processors.static', 'django.contrib.messages.context_processors.messages', 'django.core.context_processors.request',) 

AUTHENTICATION_BACKENDS = (
        'django.contrib.auth.backends.ModelBackend',
        'socialregistration.contrib.twitter.auth.TwitterAuth',
)

TWITTER_CONSUMER_KEY = 'XXXXXXXXXXXXXXXXXX'
TWITTER_CONSUMER_SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXX'

SITE_ID = 1

SESSION_SERIALIZER='django.contrib.sessions.serializers.PickleSerializer'

url.py

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'GetEvangelized.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^rango/', include('rango.urls')),
    url(r'^social/', include('socialregistration.urls',
                namespace = 'socialregistration')))

模板

<body>
        <h1>MicroCelebrity Form</h1><br>
        {% load twitter %}
        {% twitter_button %}    
        <br>
        <form id="evangelized_form" method="post" action="/rango/fillform/">

            {% csrf_token %}
            {% for hidden in form.hidden_fields %}
                {{ hidden }}
            {% endfor %}

            {% for field in form.visible_fields %}
                {{ field.errors }}
                <b>{{ field.help_text }}</b><br>
                {{ field }}<br><br>
            {% endfor %}

            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>

但是,单击 Twitter 注册按钮后,我会被重定向到此:

enter image description here

该网页的网址如下:

http://example.com/social/twitter/callback/?oauth_token=_WtPXwAAAAAAgTIEAAABTim7zYE&oauth_verifier=TheZd4yPsHNCYEi6mPwIvkugirkz20yH

我的 Twitter 应用程序的设置如下:

Callback URL    http://127.0.0.1:8000/auth/twitter/callback
Sign in with Twitter    Yes
App-only authentication https://api.twitter.com/oauth2/token
Request token URL   https://api.twitter.com/oauth/request_token
Authorize URL   https://api.twitter.com/oauth/authorize
Access token URL    https://api.twitter.com/oauth/access_token
Website  http://www.cloudclovis.com
Callback url   http://127.0.0.1:8000/auth/twitter/callback

“示例域”网页有何含义?当我点击该页面上的“更多信息”链接时,我会被重定向到 http://www.iana.org/domains/reserved

这是什么意思?我怎样才能允许我的用户在我的应用程序中使用他们的 Twitter 帐户登录?

最佳答案

它重定向到那里,因为默认 sites frameworkexample.com 作为默认站点。

如果您处于开发模式,则需要将其更改为 127.0.0.1:8000

要更改它,您可以使用默认的管理界面,或者简单地使用 django shell:

from django.contrib.sites.models import Site

site = Site.objects.get(pk=1)
site.domain = '127.0.0.1:8000'
site.save()

关于python - Django:Twitter 登录被重定向到示例域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31045016/

相关文章:

javascript - 使用 Ajax 改变点击按钮的状态

python - 在Python中,对文件使用read()后是否需要使用close()?

python - 如何在Python中返回url路径的子字符串?

python - 在 python 2.7 中连接两个嵌套的子列表

python - 按最后一个有效项目对 df 进行子集

python - 使用python脚本通过telnet在远程主机上执行命令

python - Python中列表的模式匹配

regex - django 偶尔会抛出一个 NoReverseMatch

Python 无法识别 Django 安装。 (导入错误 : No module named django)

python - 与预期不同的输出格式