如何解决if request.method == "POST": 指针没有出现在 if 语句中,有人能看出来吗?
此代码不适用于视图文件中的 post 请求,因此请任何人尝试告诉我为什么光标没有进入“POST”,请找出问题所在。
views.py
@login_required()
def candidate_view(request):
if request.method == "POST":
can = candidate.objects.filter(position = 'President')
return render(request,'poll/candidate.html',{'can':can})
else:
return render(request,'poll/candidate.html')
候选人.html
{% extends 'base.html' %}
{% block title %}Candidates{% endblock %}
{%block body%}
<h2>Available Candidates of {{ obj.title }}</h2>
<form action="" method="POST">
{% csrf_token %}
{% for c in can.candidate_set.all %}
<!-- <input type="radio" name="{{ c.position}}" value="{{c.id}}" required> <strong>{{c.name}} <a href="{% url 'detail' c.id %}">Detail</a></strong>
<br> -->
<div class="custom-control custom-radio">
<input type="radio" id="id_{{c.id}}" name="{{ c.full_name}}" value="{{c.id}}" class="custom-control-input" required>
<label class="custom-control-label" for="id_{{c.id}}">{{c.full_name}}</label>
</div>
{% empty %}
<p>No Candidates Available</p>
{% endfor %}
<br><input type="submit" class="btn btn-outline-success btn-sm" value="Vote">
</form>
<br><p><a href="{% url 'Vote' %}">Back to Poll</a></p>
{% endblock %}
投票.html
{% extends 'base.html' %}
{% block title %}Positions{% endblock %}
{%block body%}
<form action="" method="POST">
{%csrf_token%}
<ul>
<li><h2><a href="{% url 'candidate_view' %}"> President</a></h2></li>
<li><h2><a href="#"> Vice President </a></h2></li>
<li><h2><a href="#"> Secratary </a></h2></li>
<li><h2><a href="#"> Vice Secratary </a></h2></li>
</ul>
</form>
{% endblock %}
urls.py
from django.contrib import admin
from django.urls import path,include
from . import views
urlpatterns = [
path('',views.home,name="home"),path('register',views.register,name="register"),path('login',views.view_login,name="view_login"),path('logout',views.view_logout,name="view_logout"),path('candidate_view',views.candidate_view,name="candidate_view"),path('Vote',views.Vote,name="Vote"),path('result',views.result,name="result"),]
解决方法
替换
<form action="" method="POST">
{%csrf_token%}
<ul>
<li><h2><a href="{% url 'candidate_view' %}"> President</a></h2></li>
<li><h2><a href="#"> Vice President </a></h2></li>
<li><h2><a href="#"> Secratary </a></h2></li>
<li><h2><a href="#"> Vice Secratary </a></h2></li>
</ul>
</form>
与
<form action="{% url 'candidate_view' %}" method="POST">
{%csrf_token%}
<label for="President">President</label>
<input type="radio" id="President" name="position" value="President" />
<label for="Vice-President">Vice President</label>
<input type="radio" id="Vice-President" name="position" value="Vice President" />
( similarly for secretary and vice secretary )
<button>Submit</button>
</form>
同时用
更新view.py
def candidate_view(request):
if request.method == "POST":
can = candidate.objects.filter(position = request.POST.get("position"))
return render(request,'poll/candidate.html',{'can':can})
else:
return render(request,'poll/vote.html')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。