简体   繁体   English

如何使用模态调用views.py的function<form action="“" ”> ?</form>

[英]how to call function of views.py using modal <form action=“ ”>?

I have a login modal in base.html.我在 base.html 中有一个登录模式。 on clicking the login button modal gets open.单击登录按钮时,模式会打开。 I want that when the form has submitted the action='login' should call the login function of views.py but when I try to submit, it redirects the page to the login page which does not exist ('http://127.0.0.1:8000/login').我希望当表单提交时 action='login' 应该调用 views.py 的登录 function 但是当我尝试提交时,它将页面重定向到不存在的登录页面('http://127.0. 0.1:8000/登录')。 I want to know how can I call login function of views from modal, if I'm not wrong then action='' attribute calls the function and not the page.我想知道如何从模态调用视图的登录 function,如果我没有错,那么 action='' 属性调用 function 而不是页面。 I tried removing the path of login from urls.py.我尝试从 urls.py 中删除登录路径。

  1. base.html底座.html
<div class="modal fade" id="modalLoginForm">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
                <!-- Default form login -->
                <form class="text-center border border-light" action="login" method="post" autocomplete="off">
                    {% csrf_token %}

                    <!-- Email -->
                    <input type="text" name="username" class="form-control mb-4" placeholder="E-mail" required>

                    <!-- Password -->
                    <input type="password" name="password" class="form-control mb-4" placeholder="Password" required>

                    <div class="d-flex justify-content-around">
                        <div>
                            <!-- Remember me -->
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" id="defaultLoginFormRemember" name="remember_me">
                                <label class="custom-control-label" for="defaultLoginFormRemember">Remember me</label>
                            </div>
                        </div>
                        <div>
                            <!-- Forgot password -->
                            <a href="">Forgot password?</a>
                        </div>
                    </div>

                    <!-- Sign in button -->
                    <input class="btn btn-info btn-block my-3" type="submit" value="Sign in">

                </form>
                <!-- Default form login -->
            </div>
        </div>
    </div>
  1. views.py视图.py
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')

  1. app urls.py应用程序 urls.py

urlpatterns = [
    path('', views.login, name='login'),
    path('register',views.register, name='register'),
    path('logout', views.logout, name='logout')
]

  1. project urls.py项目网址.py

urlpatterns = [
    path('', include('camroid_app.urls')),
    path('accounts/', include('accounts_app.urls')),

    path('admin/', admin.site.urls),
]

how can i call login() of views from <form action='login'>我如何从<form action='login'>调用视图的 login()

as it is raising error: The current path, login, didn't match any of these.因为它引发错误: The current path, login, didn't match any of these.

how can i call function of views directly from <form action='login'>如何直接从<form action='login'>调用视图的 function

try:尝试:

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

or或者

action="/"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM