Django |使用ajax(jquery)显示选择选项的名称值而不是id

发布于 2025-01-15 00:54:18 字数 1723 浏览 0 评论 0原文

这是ajax代码

    $('.addtasksubmit').click(function(e) {
        e.preventDefault();
        

        $.ajax({
            url: 'addtodo',
            type: "POST",
            data: {
                title: $('#task-title').val(),
                category: $('#catsel option:selected').val(),
                'csrfmiddlewaretoken':$('input[name=csrfmiddlewaretoken]').val()
            },
            success: function(response) {
                console.log(response)
                $('.todos-box').append(`
                  <a href="#" class="category-link">${response.category}</a>
                `)
                $("#addtodo").trigger('reset');
                $("#exampleModal").modal('hide');
            }
        })
    })

所以 ${response.category} 给了我类别的ID,但我需要查看类别名称

,这是views.py

        response = {
        'title': request.POST['title'],
        'category' : request.POST['category'],
        }
        todo = Todo.objects.create(title=response['title'],
                                     category=Category.objects.get(id=response['category']),
                                     user=request.user)   
        todo.save()
   return JsonResponse(response, safe=False)

这是HTML

<select name="category" class="form-select form-select-sm" id="catsel">
            <option seleted>Choose Category</option>
            {% for category in categories %}
            <option value="{{ category.id }}">{{ category }}</option>
            {% endfor %}
</select>

有没有办法用ajax查看类别名称而不是id?或者我如何获取类别的名称及其 ID。

抱歉我的英语不好..

this is the ajax code

    $('.addtasksubmit').click(function(e) {
        e.preventDefault();
        

        $.ajax({
            url: 'addtodo',
            type: "POST",
            data: {
                title: $('#task-title').val(),
                category: $('#catsel option:selected').val(),
                'csrfmiddlewaretoken':$('input[name=csrfmiddlewaretoken]').val()
            },
            success: function(response) {
                console.log(response)
                $('.todos-box').append(`
                  <a href="#" class="category-link">${response.category}</a>
                `)
                $("#addtodo").trigger('reset');
                $("#exampleModal").modal('hide');
            }
        })
    })

So ${response.category} gives me the id of the category but i need the category name to be viewed

and this is views.py

        response = {
        'title': request.POST['title'],
        'category' : request.POST['category'],
        }
        todo = Todo.objects.create(title=response['title'],
                                     category=Category.objects.get(id=response['category']),
                                     user=request.user)   
        todo.save()
   return JsonResponse(response, safe=False)

this is HTML

<select name="category" class="form-select form-select-sm" id="catsel">
            <option seleted>Choose Category</option>
            {% for category in categories %}
            <option value="{{ category.id }}">{{ category }}</option>
            {% endfor %}
</select>

is there any way to view the category name with ajax not the id? or how can i get the name of the category with its id.

sorry for my bad english..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2025-01-22 00:54:18

经过 5 天的尝试,我解决了这个问题。

将其添加到视图

        response['category']=Category.objects.filter(id=category)
                           .values('name').first()
        data = json.dumps(response)
        return HttpResponse(data)

和 ajax

                <div class="category-list flex-1">
                  <a href="#" class="category-link">${response['category'].name}</a>
                </div> 

中,并添加了 dataType: 'json'

so i have solved this problem after 5 days of trying.

added this to the views

        response['category']=Category.objects.filter(id=category)
                           .values('name').first()
        data = json.dumps(response)
        return HttpResponse(data)

and ajax

                <div class="category-list flex-1">
                  <a href="#" class="category-link">${response['category'].name}</a>
                </div> 

and also added dataType: 'json'

苏璃陌 2025-01-22 00:54:18

从模板中获取包含类别的 HTML 选择输入会很有帮助。根据您发布的内容,请将views.py调整为

  todo = Todo.objects.create(title=response['title'],
  category = Category.objects.get(id=response['category']),
                                 user=request.user)   
  todo.save()  

  response = {
      'title': request.POST['title'],
      'category' : category.name,
  }


return JsonResponse(response, safe=False)

It would be helpful to get the HTML select input with the categories from the template. Based on what you posted please adjust the views.py to

  todo = Todo.objects.create(title=response['title'],
  category = Category.objects.get(id=response['category']),
                                 user=request.user)   
  todo.save()  

  response = {
      'title': request.POST['title'],
      'category' : category.name,
  }


return JsonResponse(response, safe=False)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文