我总是从request.POST.get-in-django中得不到任何消息
nam和mob字段是用户输入字段,我使用它们进行用户输入,然后我使用它们进行过滤我总是从request.POST.get-in-django中得不到任何消息,django,Django,nam和mob字段是用户输入字段,我使用它们进行用户输入,然后我使用它们进行过滤 {% block content %} <form class="form-signin" action="" method="POST"> {% csrf_token %} <div class="mb-3"> <input type="text" name="nam" id="nam" class="form-control-sm center-b
{% block content %}
<form class="form-signin" action="" method="POST">
{% csrf_token %}
<div class="mb-3">
<input type="text" name="nam" id="nam" class="form-control-sm center-block" placeholder="Nam" autofocus>
</div>
</div>
<div class="mb-3">
<select class="custom-select center-block" name="mob" id="mob" >
<option>{{ customer.sponsor }}</option>
{% for i in sponsor %}
<option value="{{ i.mobile }}"> {{ i.mobile|add:' - '|add:i.name }} </option>
{% endfor %}
</select>
<div class="invalid-feedback">
Please select a valid Existing Customer.
</div>
</div>
<div class="mb-3">
<a href="{% url 'customer_view' %}" class="btn btn-primary btn-sm" role = "button">Search</a>
Views.py
def customer_view(request):
print(request.method )
name1 = str(request.POST.get('nam'))
print(name1)
mobile1 = str(request.POST.get('mob'))
print(mobile1)
customers_list = customer.objects.filter(
mobile=mobile1) & customer.objects.filter(name=name1)
sponsors = customer.objects.all().distinct('mobile')
ctx = { 'customer': customers_list, 'sponsor': sponsors }
return render(request, 'pages/customer_view.html', ctx)
在表单中,您可以编写一个按钮,如下所示:
<a href="{% url 'customer_view' %}" class="btn btn-primary btn-sm" role="button">Search</a>
但这意味着这只是一个链接,链接到一个新的页面。因此,浏览器将对给定url发出(空)GET请求,并且从不提交表单
您可以构造一个提交表单的按钮,其中包含:
<form class="form-signin" action="{% url 'customer_view' %}" method="post">
<!-- … -->
<button type="submit" class="btn btn-primary btn-sm" role="button">Search</button>
<!-- … -->
</form>
搜寻
也就是说,搜索通常是通过GET请求完成的,因此您可能希望更改
method=“GET”
,并通过request.GET.GET(…)
获取参数。您使用的href
不提交表单。您需要一个提交
按钮,并将表单的操作
更改为查看url。试试这个:
{% block content %}
<form class="form-signin" action="{% url 'customer_view' %}" method="POST">
{% csrf_token %}
<div class="mb-3">
<input type="text" name="nam" id="nam" class="form-control-sm center-block" placeholder="Nam" autofocus>
</div>
</div>
<div class="mb-3">
<select class="custom-select center-block" name="mob" id="mob" >
<option>{{ customer.sponsor }}</option>
{% for i in sponsor %}
<option value="{{ i.mobile }}"> {{ i.mobile|add:' - '|add:i.name }} </option>
{% endfor %}
</select>
<div class="invalid-feedback">
Please select a valid Existing Customer.
</div>
</div>
<div class="mb-3">
<input type="submit" value="Search"/>
.
.
...
{%block content%}
{%csrf_令牌%}
{{customer.shandor}}
{赞助商%%中的i的百分比}
{{i.mobile | add:'-'| add:i.name}
{%endfor%}
请选择有效的现有客户。
.
.
...
您可以添加代码生成的输出吗?这将很有用您的
不是提交按钮,它只是一个href,因此您执行GET,而不是POST。输出:[23/Oct/2019 13:01:02]“GET/customer\u view HTTP/1.1”200 8191 GET NoneNone@Biswajit:确切地说,这是一个GET请求,而不是POST请求。Sir的名称为1=str(request.GET.GET('nam'))mobile1=str(request.GET.GET('mob'))同时,我没有得到任何结果。谢谢你先生澄清我的疑问和我的问题。
{% block content %}
<form class="form-signin" action="{% url 'customer_view' %}" method="POST">
{% csrf_token %}
<div class="mb-3">
<input type="text" name="nam" id="nam" class="form-control-sm center-block" placeholder="Nam" autofocus>
</div>
</div>
<div class="mb-3">
<select class="custom-select center-block" name="mob" id="mob" >
<option>{{ customer.sponsor }}</option>
{% for i in sponsor %}
<option value="{{ i.mobile }}"> {{ i.mobile|add:' - '|add:i.name }} </option>
{% endfor %}
</select>
<div class="invalid-feedback">
Please select a valid Existing Customer.
</div>
</div>
<div class="mb-3">
<input type="submit" value="Search"/>
.
.
...