为了账号安全,请及时绑定邮箱和手机立即绑定

Django 视图:根据模式名称将用户重定向到模板(从 URL 获取)

Django 视图:根据模式名称将用户重定向到模板(从 URL 获取)

慕的地10843 2023-08-22 17:47:24
我正在尝试根据用户的架构名称将用户重定向到各种应用程序。到目前为止我已经写过这个:def loginUser(request, url):        schema_list1 = ['pierre']    schema_lsit2 = ['itoiz']    if request.method == 'POST':        form = AuthenticationForm(data=request.POST)        t = urlparse(url).netloc        dbschema = '"' + t.split('.', 1)[0] + '"'        if form.is_valid():            user = form.get_user()            login(request, user)                                   if dbschema in schema_list1:                print('yes')                redirect = '/upload.html'                return HttpResponseRedirect(redirect)                        elif dbschema in schema_list2:                print('yes')                redirect = '/dash2.html'                return HttpResponseRedirect(redirect)                    else:        form = AuthenticationForm()    context = {        'form': form,        }    return render(request, 'loginUser.html', context)我的问题是我收到错误:TypeError at /loginUserloginUser() missing 1 required positional argument: 'url'Request Method: GETRequest URL:    https://itoiz.exostock.net/loginUserDjango Version: 3.0.5Exception Type: TypeErrorException Value:    loginUser() missing 1 required positional argument: 'url'Exception Location: /home/ubuntu/exo/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response, line 113我对 django 相当陌生,我很困惑为什么会收到此错误,特别是因为我在其他地方使用了完全相同的方法并且效果很好。我想知道是否有人能看到我所缺少的东西。顺便问一下,还有其他方法获取用户的网址吗?
查看完整描述

2 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

请执行以下操作:

def login_user(request):  # we don't do camel-casing

    

    # I don't know your exact use case, but having this in the view seems wrong..

    schema_list1 = ['pierre']

    schema_lsit2 = ['itoiz']


    # This will be an empty "{}" if it's not a POST, meaning, you don't need the 

    # extra if, it will work in both cases

    form = AuthenticationForm(data=request.POST)

    if request.method == 'POST':

        dbschema, _, _ = request.get_host().partition('.')

        if form.is_valid():

            user = form.get_user()

            login(request, user)

            

            if dbschema in schema_list1:

                print('yes')

                redirect = '/upload.html'

                return HttpResponseRedirect(redirect)

            elif dbschema in schema_list2:

                print('yes')

                redirect = '/dash2.html'

                return HttpResponseRedirect(redirect)

            else:

                raise Exception('HANDLE THIS!!!')


    context = {

        'form': form,

    }

    return render(request, 'loginUser.html', context)


查看完整回答
反对 回复 2023-08-22
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

当您提交表单时 - 您可能会向“loginUser”发送不带参数的请求。

另一件事是,您可能不需要 URL 作为参数,您可以简单地从请求中获取它:

request.build_absolute_uri()


查看完整回答
反对 回复 2023-08-22
  • 2 回答
  • 0 关注
  • 104 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信