我是 django 的新手,我遇到了一些困难。我有一张包含多条(100 条)记录的表。
我想使用 jQuery 来启用记录的内联更新和删除。
HTML:
<p>The current candidate list is:</p>
<div id="display1">
<span>
<button class="delete_button" id="1">del</button>
</span>
<span style="width:50px;">
<button class="editdiv" id="1">EDIT</button>
</span>
<span>
<a href="/candidates/1/">jake wilmott</a>
</span>
<span>
de vere
</span>
<span>
Nepal
</span>
<span>
hands off
</span>
<span>
connected
</span>
<span>
nice guy
</span>
</div>
<div class="noshow" id="edit1">
<span style="width:100px;"><button class="update" id="1">update</button></span>
<span>
jake wilmott
</span>
<span style="width:220px;">
<input class="company" type="textfield" style="width:210px;" id="new_company1" value="de vere" />
</span>
<span>
<input type="text" id="new_country1" value="Nepal" />
</span>
<span>
<select id="new_status1">
<option value="no contact">no contact</option>
<option value="hands off">hands off</option>
<option value="ongoing">ongoing</option>
<option value="sent email">sent email</option>
<option value="waiting">waiting</option>
<option value="trash">trash</option>
</select>
</span>
<span>
<input type="text" id="new_connection" value="connected" />
</span>
<span style="width:220px;">
<input type="textfield" style="width:210px;" id="new_notes" value="nice guy" />
</span>
</div>
jQuery函数是:
//delete
<script>
$(document).ready(function() {
$(".delete_button").click(function() {
alert('delete script')
var id = $(this).attr('id');
alert(id)
$.ajax({
type: "POST",
url: "/candidates/",
data: { id:id },
success: function(response){
alert(response);
}
});
return false;
});
});
</script>
alert('delete script')
警报等等alert(id)
。但后来它崩溃了,什么也没有发生
更新的 jQuery 是:
<script>
$(document).ready(function() {
$('.update').click(function() {
alert('script now')
var id = $(this).attr('id');
var company = $("#new_company" + id).val()
var country = $("#new_country" + id).val()
var status = $("#new_status" + id).val()
alert(status)
$.post('/candidates/' + id + company + country + status + '/', function() {
alert('to here')
//$this.replaceWith("<span class='success'>Liked</span>");
jQuery(data["html"]).appendTo(".success");
});
});
});
</script>
再次alert('script now')
并且alert(status)
都尽职尽责地警觉,然后它再次崩溃。
urls.py 是:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
# Examples:
url(r'^candidates/$', 'candidates.views.index'),
# url(r'^$', 'amore.views.index', name='index'),
url(r'^candidates/(?P<id>\d+)/$', 'candidates.views.detail'),
url(r'^candidates/(?P<id>\d+)/$', 'candidates.views.delete'),
url(r'^update/(\d+)/$','candidates.views.update'),
view.py 是:
from django.template import Context, loader
from candidates.models import Candidates
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
def index(request):
return HttpResponse("Hello, world. You're at the candidate index.")
def index(request):
data = Candidates.objects.all()[:5]
t = loader.get_template('candidates/index.html')
c = Context({
'data': data,
})
return HttpResponse(t.render(c))
def update(request, id):
candidate = Candidates.objects.get(pk = id)
candidate.company = request.POST.get('company')
candidate.country = request.POST.get('country')
candidate.status = request.POST.get('status')
candidate.notes = request.POST.get('notes')
candidate.save()
return HttpResponse('updated')
def delete(request, id):
candidate = Candidates.objects.get(pk = id)
candidate.delete()
return HttpResponse('this record has been deleted')
def detail(request, id):
p = get_object_or_404(Candidates, pk=id)
return render_to_response('candidates/detail.html', {'Candidates': p})
我已经搜索了数百页,但我无法解决这个问题。
所以:
- 我究竟做错了什么?
- 将变量传递给 jQuery 脚本中的 URL 应该是什么
views.py
- 文件是否
urls.py
正确?
请帮我让它工作!