Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用modal调用views.py函数<;形式动作="&引用>;?_Python_Python 3.x_Django - Fatal编程技术网

Python 如何使用modal调用views.py函数<;形式动作="&引用>;?

Python 如何使用modal调用views.py函数<;形式动作="&引用>;?,python,python-3.x,django,Python,Python 3.x,Django,我在base.html中有一个登录模式。单击“登录”按钮后,modal将打开。我希望表单提交后,action='login'应该调用views.py的login函数,但当我尝试提交时,它会将页面重定向到不存在的登录页面('login')http://127.0.0.1:8000/login'). 我想知道如何从modal调用视图的login函数,如果我没有错,那么action=''属性调用函数而不是页面。我尝试从URL.py中删除登录路径 base.html app url.py 项目URL.p

我在base.html中有一个登录模式。单击“登录”按钮后,modal将打开。我希望表单提交后,action='login'应该调用views.py的login函数,但当我尝试提交时,它会将页面重定向到不存在的登录页面('login')http://127.0.0.1:8000/login'). 我想知道如何从modal调用视图的login函数,如果我没有错,那么action=''属性调用函数而不是页面。我尝试从URL.py中删除登录路径

  • base.html
  • app url.py
  • 项目URL.py
  • 如何从

    正在引发错误:
    当前路径login与这些路径中的任何一个都不匹配。

    如何直接从

    调用视图函数请尝试:

    action="{% url 'login' %}"
    


    为什么您希望它是/login/如果您的URL指向它是/accounts/您可以添加另一个路径路径('login',views.login,name='login'),我实际上没有任何login.html页面。所以不需要创建任何路径将其重定向到登录页面。相反,我想直接调用表单submit上的登录函数,我认为这会起作用。因为它不会引发任何错误,但也不会调用登录函数。
    def login(request):
        if request.method == "POST":
            username = request.POST['username']
            password = request.POST['password']
    
            user = auth.authenticate(username=username, password=password)
    
            if user is not None:
                auth.login(request, user)
                return redirect('/')
            else:
                messages.info(request, 'invalid credentials')
                return redirect('index')
        else:
            return render(request, 'index.html')
    
    
    
    urlpatterns = [
        path('', views.login, name='login'),
        path('register',views.register, name='register'),
        path('logout', views.logout, name='logout')
    ]
    
    
    
    urlpatterns = [
        path('', include('camroid_app.urls')),
        path('accounts/', include('accounts_app.urls')),
    
        path('admin/', admin.site.urls),
    ]
    
    action="{% url 'login' %}"
    
    action="/"