我正在尝试使用一个显示客户下拉列表的表单。当用户选择客户时,我想使用所选择的客户名称在同一个html页面中执行某些操作。我使用POST方法,我收到Method Not Allowed (POST): /opsform/
错误。如果我使用GET,则URL具有我选择的值,但不会触发views.py中的getCustName
方法。我搜索得足够多,并尝试了所有建议,但无法完成这项工作。以下是相关文件。请帮忙。
view.py
from django.shortcuts import render, get_object_or_404
from django.views import generic
from opsform.models import Customer, Tracking
from django.http.response import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
# Create your views here.
class DetailCustomer(generic.ListView):
template_name = 'customerdrop.html'
context_object_name = 'customerdrop'
def get_queryset(self):
"""Return the all the customer information."""
return Customer.objects.all()
def getCustName(request,cust_name):
customer = get_object_or_404(Customer, pk=cust_name)
selected_track = customer.tracking_set.get(pk=request.GET['cust_name'])
selected_track.votes += 1
selected_track.save()
return HttpResponseRedirect(reverse('opsform:vote', args=(customer.customer_id,)))
Urls.py
from django.conf.urls import url
from . import views
app_name='opsform'
urlpatterns = [
url(r'^$', views.DetailCustomer.as_view(), name='customerdrop'),
]
customerdrop.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<h1>Choose the customer you are working on</h1>
<form action="" method="post">
<div class="form-group">
{% csrf_token %}
{% if customerdrop %}
<select class="form-control" name="cust_name">
{% for customer in customerdrop %}
<option value="{{ customer.customer_id }}">{{ customer.customername }}</option>
{% endfor %}
</select>
{% else %}
<p>No Customer are available.</p>
{% endif %}
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</body>
</html>