【问题标题】:(DJANGO) How to be redirect to another URL from a view?(DJANGO) 如何从视图重定向到另一个 URL?
【发布时间】:2020-02-10 15:14:25
【问题描述】:

我是 Django 新手,现在在从当前视图重定向到另一个 URL 时遇到了一些问题。在这种情况下,我想被重定向到 Spotify 登录页面。

这是我的看法:

#############################################################################
client_id = 'somestring'; # Your client id
client_secret = 'anotherstring'; # Your secret
redirect_uri = 'http://127.0.0.1:8000/callback/'; # Your redirect uri
stateKey = 'spotify_auth_state'
#############################################################################

def generateRandomString(length):
    text = ''
    possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'

    for i in range(0,length):
         text += possible[math.floor(random.random() * len(possible))] 
    return text

##############################################################################


def login_process(request):

    if request.method == 'GET':

        state = generateRandomString(16)
        print(str(state))
        HttpResponse.set_cookie(stateKey, state)

        #your application requests authorization
        scope = 'user-top-read user-read-email'
        return HttpResponseRedirect(request, 'https://accounts.spotify.com/authorize?' + urllib.parse.urlencode({
          response_type: 'code',
          client_id: client_id,
          scope: scope,
          redirect_uri: redirect_uri,
          state: state
        }), {})

def login_view(request, *args, **kwargs):
    print(args, kwargs)
    print(request.user)
    #return HttpResponse("<h1>Hello world</h1>")
    return render(request, "login.html", {})




def callback_view(request, *args, **kwargs):
    return render(request, "callback.html", {})


这是我应该点击重定向的链接:

    <a href="login/">Login with spotify</a>

这是我的 urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', login_view, name='login_view'),
    path('login/', login_process, name = 'login'),
    path('callback/', callback_view, name = 'callback_view'),
]

我得到的错误是“AttributeError at /login/'str' object has no attribute 'cookies'”,我什至不知道“return HttpResponseRedirect”方法是否是做所有这些事情的正确方法。你能帮帮我吗?

【问题讨论】:

    标签: python django spotify


    【解决方案1】:

    这里:

    HttpResponse.set_cookie(stateKey, state)
    

    您是在类本身而不是实例上调用HttpResponse.set_cookie,因此您会得到一个未绑定的方法,该方法需要一个实例作为第一个参数。正确的做法其实是先实例化响应,然后调用set_cookie就可以了:

    qs = urllib.parse.urlencode({
              "response_type": 'code',
              "client_id": client_id,
              "scope": scope,
              "redirect_uri": redirect_uri,
              "state": state
            })
    url = 'https://accounts.spotify.com/authorize?{}'.format(qs) 
    response = HttpResponseRedirect(request, url)
    reponse.set_cookie(whatever)
    return response
    

    【讨论】:

    • 感谢您的回答,但我登录时仍然出现错误,它说“异常类型:NameError at /login/ 异常值:名称'response_type'未定义”
    • @DvD_95 dict 键周围的引号确实丢失了(已修复)-但在您的原始 sn-p (我复制/粘贴)中已经是这种情况。另请注意,这通常是任何开发人员都应该能够自己发现和修复的微不足道的错误......
    • 非常感谢!是的,我知道,经过一些调试,我发现了它,在某些时候它不可能是这样的!哈哈
    猜你喜欢
    • 2015-06-21
    • 2018-10-26
    • 2020-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多