我正在尝试使用Django编辑表中的条目。必要的代码如下:
urls.py
from django.conf.urls import patterns, url, include
from updatedb import views
urlpatterns = patterns('',url(r'^$', views.index, name='index'),
url(r'^submit', views.submit, name='submit'),
url(r'^edit', views.edit, name='edit'),
url(r'^editdb', views.editdb, name='editdb'),
)
views.py
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from updatedb.models import Data
from django.core.urlresolvers import reverse
from django.views.decorators.csrf import csrf_exempt
def index(request):
return render(request, 'updatedb/data.html')
def submit(request):
first = request.POST["first"]
second = request.POST["second"]
d = Data()
d.first = first
d.second = second
print first
print second
d.save()
if request.POST.get("submit"):
qset = Data.objects.all()
return render(request, 'updatedb/results.html', {'queryset':qset})
@csrf_exempt
def edit(request):
qset = Data.objects.all()
return render(request, 'updatedb/edit.html', {'queryset':qset})
@csrf_exempt
def editdb(request):
print "hi"
print request.POST.get["data"]
return render(request, 'updatedb/editdb.html')
edit.html
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
function clickable(ele)
{
var value = prompt("Enter the details");
if(value)
{
ele.innerHTML = value;
var xmlhttp;
$.ajax({
url : 'editdb',
type: "POST",
data: {data: value},
success: function(data){
if(!data)
alert("Data:"+data);
},
error: function(){
alert("Error in the request");
}
});
//$.post("editdb.html", {data: value});
}
}
</script>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Phone Number</th>
</tr>
{% for record in queryset %}
<tr>
<td onClick="clickable(this)"> {{record.first}} </td>
<td onClick="clickable(this)"> {{record.second}}</td>
</tr>
{%endfor%}
</table>
</body>
</html>
理想情况下,在编辑表中的值时,应调用editdb视图,但是这没有发生。
错误警报说,正在生成请求中的错误。
我要去哪里错了?
最佳答案
您的网址格式不正确。 edit
URL还将始终与editdb
匹配,因此将永远无法访问另一个URL。确保终止它们:
url(r'^submit$', views.submit, name='submit'),
url(r'^edit$', views.edit, name='edit'),
url(r'^editdb$', views.editdb, name='editdb'),