从 Django 中的按钮调用函数
Call function from button in django
在我的一个页面上,我想显示一个按钮,每当单击此按钮时,我想在我的显示器中显示以下内容 "Button clicked"。
但是我的控制台中显示以下消息。""GET /account/all-plan/?print_btn=Click HTTP/1.1" 200 5025"
这是我的看法
def print_from_button(request):
if(request.GET.get('print_btn')):
print('Button clicked')
return HttpResponse('testklik')
html
<form method="get">
<input type="submit" class="btn" value="Click" name="print_btn">
</form>
和 url 在 urls.py
path('all-plan/print_from_button', views.print_from_button, name='print_from_button'),
任何人都可以指出我正确的方向,我找不到我缺少的东西。非常感谢!
您似乎必须 URLs:
/account/all-plan/
/account/all-plan/print_from_button
在第一个 URL 中,您正在创建一个 <form>
,它使用 GET
方法,但没有 action
attribute is specified. The result is that your form submits to the same URL as the current page (first URL). This can be seen as your console print says it's using the first URL with an extra GET parameter.
为了让您的表单使用正确的 URL,您需要使用正确的 URL:
指定 action
属性
<form method="get" action="{% url "app_name:print_from_button" %}">
...
</form>
在我的一个页面上,我想显示一个按钮,每当单击此按钮时,我想在我的显示器中显示以下内容 "Button clicked"。
但是我的控制台中显示以下消息。""GET /account/all-plan/?print_btn=Click HTTP/1.1" 200 5025"
这是我的看法
def print_from_button(request):
if(request.GET.get('print_btn')):
print('Button clicked')
return HttpResponse('testklik')
html
<form method="get">
<input type="submit" class="btn" value="Click" name="print_btn">
</form>
和 url 在 urls.py
path('all-plan/print_from_button', views.print_from_button, name='print_from_button'),
任何人都可以指出我正确的方向,我找不到我缺少的东西。非常感谢!
您似乎必须 URLs:
/account/all-plan/
/account/all-plan/print_from_button
在第一个 URL 中,您正在创建一个 <form>
,它使用 GET
方法,但没有 action
attribute is specified. The result is that your form submits to the same URL as the current page (first URL). This can be seen as your console print says it's using the first URL with an extra GET parameter.
为了让您的表单使用正确的 URL,您需要使用正确的 URL:
指定action
属性
<form method="get" action="{% url "app_name:print_from_button" %}">
...
</form>