django 从 AJAX 调用返回不会更新HTML

ercv8c1e  于 2023-01-14  发布在  Go
关注(0)|答案(1)|浏览(177)

我正在更新一个数据库条目通过点击选择下拉菜单。确认选择后,我发送一个 AJAX POST到另一个视图,应用所需的数据库更改,并返回到上一页。
问题是目标页面不反映新值,除非我刷新。
选择HTML(项目. html)

<select id="update_status_outreach" name="{{outreach.id}}_____{{project.id}}">
    {% for outreach_status in outreach_statuses %}
        {% if outreach_status.0 == outreach.outreach_status %}
            <option value="{{ outreach.outreach_status }}" selected>{{ outreach.outreach_status }}</option>
        {%else%}
            <option value="{{ outreach_status.0}}">{{outreach_status.0}}</option>
        {% endif %}
    {%endfor%}
</select>

JavaScript(项目. html)

document.getElementById("update_status_outreach").onchange = changeListener;
function changeListener() {
    var name = this.name.split("_____");
    var project_id = name[1];
    var outreach_id = name[0];
    var new_status = this.value;
    if(confirm("Placejpòdore")) {
        $.ajax({
            type: "POST",
            url: "{% url 'action:update_status_outreach' %}",
            data: {
                csrfmiddlewaretoken: "{{ csrf_token }}",
                'project_id': project_id,
                'outreach_id': outreach_id,
                'new_status': new_status
            },
        });
    }
}

www.example.com中的更新状态外展视图outreach.py

@login_required(login_url='action:login')
def update_status_outreach(request):
    project_id = request.POST['project_id']
    outreach_id = request.POST['outreach_id']
    new_status = request.POST['new_status']
    jobproject = get_object_or_404(JobProject, id=project_id)
    if jobproject.owner != request.user:
        raise HttpResponse(status=500)
    else:
        outreach_to_be_updated = Outreach.objects.get(id=outreach_id)
        outreach_to_be_updated.outreach_status=new_status
        outreach_to_be_updated.save()
        return HttpResponseRedirect(reverse('action:project', args=(project_id,)))

www.example.com中的项目视图project.py

@login_required(login_url='action:login')
def project(request, project_id):
    if request.method == 'POST':
        outreach_id = request.POST.get('outreach_id')
        new_rank = request.POST.get('new_ranking')
        update_rank(outreach_id=outreach_id, new_rank=new_rank)
    project_referred = get_object_or_404(JobProject, id=project_id)
    jobpositions_referred = JobPosition.objects.filter(jobproject=project_referred)
    outreaches_referred = Outreach.objects.filter(position__jobproject=project_referred)
    colors = ['#9bf5ff', '#a89dff', '#fff79d', '#9b5252', '#72b267', '#d75e5e']
    context = {'project': project_referred, 'jobpositions': jobpositions_referred, 'outreaches': outreaches_referred,
               'outreach_statuses': OUTREACH_STATUS, 'colors': colors, 'position_statuses': POSITION_STATUS}
    return render(request, 'action/project.html', context)
pod7payv

pod7payv1#

首先你需要将Django函数中的新值返回到HTML文件中,然后你需要在 AJAX 中添加一个success函数,其中select,option元素也会被更新,问题是你在加载网站时创建了一个select元素,但是没有脚本来改变HTML内容。

$.ajax({
            method: "post",
            dataType: "json",
            data: ...,
            url: "/...",
            success:function(response){
                document.GetElementById("id_of_option").innerHTML = response.your_response
                    
            }
        });

相关问题