简体   繁体   中英

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

I have a login modal in 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'). 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. I tried removing the path of login from urls.py.

  1. base.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
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

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

  1. project urls.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'>

as it is raising error: The current path, login, didn't match any of these.

how can i call function of views directly from <form action='login'>

try:

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

or

action="/"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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