私はdjangoを初めて使用し、いくつかの問題があります。複数(数百)のレコードを持つテーブルがあります。
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'),
views.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
ファイルは正しいですか?
私がそれを機能させるのを手伝ってください!