Django:在表的编辑过程中没有访问View

时间:2014-11-13 06:37:34

标签: javascript jquery python ajax django

我正在尝试使用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 视图但不会发生这种情况。

正在生成错误提示,请求中的错误

我哪里错了?

1 个答案:

答案 0 :(得分:1)

您的网址格式错误。 edit网址始终也会与editdb匹配,因此永远无法访问其他网址。确保你终止它们:

url(r'^submit$', views.submit, name='submit'),
url(r'^edit$', views.edit, name='edit'),
url(r'^editdb$', views.editdb, name='editdb'),