Django Ajax表单上的500错误

时间:2013-11-15 06:19:53

标签: jquery ajax django

我有一个正常运行的表单,但现在我还没有尝试将其转换为Ajax。我收到500错误。有什么想法吗?

初始表单HTML

<form method="post">
    {% csrf_token %}
    <input type='hidden' name='entry_id' value='{{ e.id }}'>
    <input type='hidden' name='project_id' value='{{ project.id }}'>
    <input type='hidden' name='team_id' value='{{ t.id }}'>
    {% get_vote_status request=request project=project entry=e.entry %}
</form>

上面使用的模板标签

@register.simple_tag
def get_vote_status(request, project, entry):
    user = request.user
    determined_vote_status = False

    try:
        for v in user.userprofile.votes.all():
            if v.entry == entry:
                determined_vote_status = True
                return '''<button type="submit" name="remove_vote" value="Remove Vote" class="votingbutton"><img src="/static/bootstrap/img/checked.png" alt="Voted" class="check-button"></button>'''
        if determined_vote_status is False:
            return '''<button type="submit" name="add_vote" value="Add Vote" class="votingbutton"><img src="/static/bootstrap/img/check.png" alt="Voted" class="check-button"></button>'''

    except:
    pass

更新了Ajax HTML

<script type="text/javascript">
    $(document).ready(function() {
        var csrftoken = $.cookie('csrftoken');

        function csrfSafeMethod(method) {
            // these HTTP methods do not require CSRF protection
            return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
        }
        $.ajaxSetup({
            crossDomain: false, // obviates need for sameOrigin test
            beforeSend: function(xhr, settings) {
                if (!csrfSafeMethod(settings.type)) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });
        $("#button{{ forloop.counter }}").click(function() {
            var $url = "/craig-test/testing-craig/6/5/";
            var $type = "POST";
            var $dataType = "json";
            var $data = {
            'entry_id': {{ e.id }}, 
            'project_id': {{ project.id }}, 
            'team_id': {{ t.id }},
            csrfmiddlewaretoken: csrftoken
            };

            $data = JSON.stringify($data);

            $.ajax({
                url : $url,
                type : $type,
                dataType: $dataType,
                data : $data,
                success : function(json) {
                console.log("+1");
                },
                error : function(xhr,errmsg,err) {
                console.log(errmsg, err);
                }
            });
            return false;
        });
    });
</script>

<form method="post" name="vote form" >
    {% csrf_token %}
    <input type='hidden' name='entry_id' value='{{ e.id }}'>
    <input type='hidden' name='project_id' value='{{ project.id }}'>
    <input type='hidden' name='team_id' value='{{ t.id }}'>
    <input id="button{{ forloop.counter }}" type="button" value="send to server"></input>
</form>

views.py

@login_required(login_url='/accounts/login/')
def projects(request, teamslug, projectslug,
    teamid, projectid, template_name='projects.html'):
    teams = Team.objects.all()
    projects = Project.objects.all()
    userprofile = UserProfile.objects.all()
    votes = Vote.objects.all()
    user = request.user

    for p in user.userprofile.projects.all():
        sluglist.append(p.slug)

    if projectslug in sluglist:
        if request.is_ajax():
            raise ValueError("tktktkt")

urls.py

url(r'^(?P<teamslug>[^\.]+)/(?P<projectslug>[^\.]+)/(?P<teamid>[^\.]+)/(?P<projectid>[^\.]+)/$', 'ideas.views.projects', name='project_url')

0 个答案:

没有答案