单击按钮djang时调用函数

2024-10-03 13:21:29 发布

您现在位置:Python中文网/ 问答频道 /正文

当用户点击页面上的按钮“submit”时,我想调用python函数http://junjob.ru/accounts/login/

我该怎么做?

enter image description here

我的代码:

视图.py

class BBLoginView(LoginView):
    template_name = 'vacancy_list/login.html'

class BBLogoutView(LoginRequiredMixin, LogoutView):
    template_name = 'vacancy_list/vacancy_list.html'
    next_page = reverse_lazy('vacancy_list')

url.py

urlpatterns = [

    path('accounts/login/', BBLoginView.as_view(), name='login'),
    path('accounts/profile/', profile, name='profile'),
    path('accounts/logout/', BBLogoutView.as_view(), name='logout'),
    ...

登录.html

{% block content %}
    <div class="container" style="margin-top:10px">
        <h2>Login</h2>
        {% if user.is_authenticated %}
            <p>You are already registered</p>
        {% else %}
        <form method="post">
            {% csrf_token %}
            {% bootstrap_form form layout='horizontal' %}
            <input type="hidden" name="next" value="{{ next }}">
            {% buttons submit="Submit" %} {% endbuttons %}
        </form>
       {% endif %}
    </div>
{% endblock %}

Tags: pathnamepyformhtmllogintemplateprofile
1条回答
网友
1楼 · 发布于 2024-10-03 13:21:29

您可以连接到父类LoginViewform_valid方法:

class BBLoginView(LoginView):
    template_name = 'vacancy_list/login.html'

    def form_valid(self, form):
        # Put your code here
        return super().form_valid(form)

如果只有窗体有效但在用户身份验证之前,它将触发

如果您需要在任何情况下执行代码,请挂接到post方法:

class BBLoginView(LoginView):
    template_name = 'vacancy_list/login.html'

    def post(self, request, *args, **kwargs):
        # Put your code here
        return super().post(request, *args, **kwargs)

您可以找到LoginViewhere的源代码

相关问题 更多 >