当前使用Django 2.1,Python 3.6,PostgreSQL 11,并将数据库托管在Heroku上。
我正在创建一个充当GUI数据库条目的Web应用程序。我最近Django Front End Search在这里提供了一些搜索代码。该代码与将服务器托管在我的计算机上的测试应用程序一起使用。当我尝试与Heroku托管数据库通信时,我现在看到405错误。
目前,我的网络应用在捐赠者_list.html上列出了捐赠者。我希望在用户执行查询后,将其查询结果发布在donor_list.html上。
这是错误:
不允许的方法(POST):/ device / donor_list /
不允许的方法:/ device / donor_list /
“ POST / device / donor_list / HTTP / 1.1” 405 0
# Associated urls
path('donor_list/',views.DonorList.as_view(),name='donor_list'),
path('donor_list/',views.SearchDonor,name='donor_search'),
# Donor Model
class Donor(models.Model):
name=models.CharField(max_length=265,blank=False)
email=models.EmailField(max_length=265,blank=False)
donation_house=models.ForeignKey(DonationHouse,
default='1',
related_name='dono_house',
on_delete=models.CASCADE)
def __str__(self):
return self.name
# Donor View
class DonorList(ListView):
context_object_name = 'donors'
model=models.Donor
# Search Code
def SearchDonor(request):
keywords=''
if request.method=='POST': # form was submitted
keywords = request.POST.get("ds", "")
all_queries = None
search_fields = ('name','email','donation_house__title')
for keyword in keywords.split(' '):
keyword_query = None
for field in search_fields:
each_query = Q(**{field + '__icontains': keyword})
if not keyword_query:
keyword_query = each_query
else:
keyword_query = keyword_query | each_query
if not all_queries:
all_queries = keyword_query
else:
all_queries = all_queries & keyword_query
donorsearches = Donor.objects.filter(all_queries).distinct()
context = {'donorsearches':donorsearches}
return render(request, 'device_app/donor_list.html', context)
else: # no data submitted
context = {}
return render(request, 'device_app/index.html', context)
# donor_list.html
{% extends 'device_app/base.html' %}
{% block body_block %}
<div class="jumbotron">
<h1>Donor List</h1>
<p><a class="button" href="{% url 'device_app:donor_create'%}">
Create Donor</a>
</p>
<form method="POST" action=".">
{% csrf_token %}
<input id="search_box" type="text" name="ds" placeholder="Search..." >
<button class="button" type="submit" >Submit</button>
</form>
<br>
{{donorsearches}}
<br>
<ul>
{% for donor in donors %}
<h6>
<li>
<a class="annoying" href="{{donor.id}}">{{donor.name}}</a>
</li>
</h6>
{% endfor %}
</ul>
</div>
{% endblock %}
答案 0 :(得分:0)
如果希望搜索结果显示在同一页面上,则需要将它们放在同一视图中,而不是单独的视图中。
class DonorList(ListView):
context_object_name = 'donors'
model=models.Donor
def post(self, request):
keywords = request.POST.get("ds", "")
all_queries = None
search_fields = ('name','email','donation_house__title')
for keyword in keywords.split(' '):
...
尽管要注意,对于搜索来说,通常使用GET请求而不是POST。