django没有返回适当的响应 [英] django not returning appropriate response
问题描述
Index.html:
< form method =POSTaction =/ test />
{%csrf_token%}
< div id =row-1>
< button name =packid =pack> Pack< / button>
< / div>
< div id =row-2>
< table>
< thead>
< tr>
< th< input type =checkboxonClick =toggle(this,'no')>< / th>
< th>订单ID< / th>
< / tr>
< / thead>
< tbody>
< tr>
< td>< input type =checkboxname =novalue =10000001> 10000001< / td>
< / tr>
< tr>
< td>< input type =checkboxname =novalue =10000002> 10000002< / td>
< / tr>
< / tbody>
< / table>
< / div>
< / form>
views.py:
from django.shortcuts import render,render_to_response
from django.http import HttpResponse
from test1 import forms
def index(request):
return render(request,'index.html')
def pack(request):
oid_list = []
form = forms.PackOrders(request.POST或None)
如果request.method =='POST':
for form.cleaned_data ['no']:
oid_list.append(item)
return HttpResponse(',' .join(oid_list))
urls.py:
$ b $从django.conf.urls导入模式
从django.conf.urls导入url
从test1导入视图
urlpatterns = patterns('',
url(r'^ $',views.index,name ='index'),
url(r'^ test /',views.pack,name ='pack '),)
forms.py:
从django import for ms
class PackOrders(forms.Form):
order_items = forms.CharField()
问题是,当我清理复选框,我返回代码,它只是重新加载index.html而不是显示复选框的值。
预期输出:
10000001,10000002
代码有什么问题?
编辑:
项目urls.py:
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^ admin /',include(admin.site.urls)),
url(r'^ $',include('test1.urls ')),
url(r'^ test /',include('test1.urls')),
]
您的应用程序urlconf已经包含在您的root urlconf中。你应该只做一次但是,您应该从包含它的URL中删除$:
url(r'^',include('test1。 urls')),
如果您使用第二个版本,使用'test /'前缀,您将创建'/ test /'和'/ test / test /'的URL,这可能不是你想要的。
注意,如我所说评论时,您应该始终通过其名称引用URL,而不是直接引用URL。所以你的表单动作应该是{%url'pack'%}
。
I have a template with checkbox and a button. I want , when i select all checkbox and press the button it should return the values and display it as a httpresponse.
Index.html:
<form method="POST" action="/test/">
{% csrf_token %}
<div id="row-1">
<button name="pack" id="pack">Pack</button>
</div>
<div id="row-2">
<table>
<thead>
<tr>
<th><input type="checkbox" onClick="toggle(this, 'no')"></th>
<th>Order Id</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="no" value="10000001">10000001</td>
</tr>
<tr>
<td><input type="checkbox" name="no" value="10000002">10000002</td>
</tr>
</tbody>
</table>
</div>
</form>
views.py:
from django.shortcuts import render, render_to_response
from django.http import HttpResponse
from test1 import forms
def index(request):
return render(request, 'index.html')
def pack(request):
oid_list = []
form = forms.PackOrders(request.POST or None)
if request.method == 'POST':
for item in form.cleaned_data['no']:
oid_list.append(item)
return HttpResponse(','.join(oid_list))
urls.py:
from django.conf.urls import patterns
from django.conf.urls import url
from test1 import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^test/', views.pack, name='pack'),)
forms.py:
from django import forms
class PackOrders(forms.Form):
order_items = forms.CharField()
The problem is that when i cleck the checkbox and i return the code it just reload the index.html instead of displaying the values of checkbox.
Expected Output:
10000001,10000002
What is wrong with the code?
EDIT :
projects urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('test1.urls')),
url(r'^test/', include('test1.urls')),
]
You've included your app urlconf into your root urlconf twice. You should only do it once. However, you should remove the $ from the URL that includes it:
url(r'^', include('test1.urls')),
If you used the second version instead, with the 'test/' prefix, you would create URLs at '/test/' and '/test/test/', which presumably isn't what you want.
Note though that as I said in the comment, you should always reference URLs via their name, not directly. So your form action should be "{% url 'pack' %}"
.
这篇关于django没有返回适当的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!