如何使用 Jquery 更改按钮单击时的 django 外键对象文本选择字段?

Posted

技术标签:

【中文标题】如何使用 Jquery 更改按钮单击时的 django 外键对象文本选择字段?【英文标题】:How to change djangos foreign key objects textchoicefield on button click with Jquery? 【发布时间】:2020-07-02 10:15:47 【问题描述】:

我有 2 个模型 ToDoList 和 Tasks。在 Tasks 模型中,我将 ForeignKey 设置为 ToDoList(一个 ToDoList 可以有多个任务)。

我想做的是: 在 ToDoDetailView 中显示特定 ToDoList 的所有任务(过滤状态 =“已发布”)。我想在每一个任务旁边都有一个按钮。单击该按钮时,我希望单个任务更改 status="trash"。

我想使用 ajax 并试图理解(我是新手)。有人可以帮助/指导和支持我。

models.py

from django.db import models
from django.contrib.auth import get_user_model
from django.urls import reverse

# Create your models here.

class TimeStamp(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True


class ToDoList(TimeStamp):


    class STATUS(models.TextChoices): 
        PUBLISHED = "published", "Published" 
        TRASH = "trash", "Trash"
        WORKINGDRAFT = "workingdraft", "Workingdraft" 


    headline = models.CharField(max_length=200)
    author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
    status = models.CharField("Status", max_length=20, choices=STATUS.choices, default=STATUS.PUBLISHED)



    def __str__(self):
        return self.headline

    def get_absolute_url(self):
        return reverse('notepad:todo_detail', args=[str(self.id)])


class Tasks(TimeStamp):

    class STATUS(models.TextChoices): 
        PUBLISHED = "published", "Published" 
        TRASH = "trash", "Trash"
        WORKINGDRAFT = "workingdraft", "Workingdraft" 


    todos = models.CharField(max_length=250)
    todolist = models.ForeignKey(ToDoList, on_delete=models.CASCADE, related_name='tasks')
    status = models.CharField("Status", max_length=20, choices=STATUS.choices, default=STATUS.PUBLISHED)


    def __str__(self):
        return self.todos

views.py

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponseRedirect, HttpResponse
from django.forms.models import inlineformset_factory
from django.views import View
from django.views.generic import ListView, DetailView, TemplateView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse, reverse_lazy
from django.db import transaction
from django.contrib.auth.mixins import LoginRequiredMixin


from .models import ToDoList, Tasks
from .forms import  TasksForm, TasksFormSet, ToDoListFirstForm, ToDoListForm

class ChangeTaskStatusView(View):
    def get(self, request):
        task = request.GET['task']
        Tasks.objects.filter(id = task).update(status='trash')
        return HttpResponse(task)

class ToDoListView(LoginRequiredMixin, ListView):
    model = ToDoList
    #queryset = ToDoList.objects.filter(tasks__status="published")
    context_object_name = 'todos_list'
    template_name = 'notepad/notepad_list.html'
    paginate_by = 2

    def get_queryset(self):
        return ToDoList.objects.filter(author=self.request.user)

class ToDoDetailView(LoginRequiredMixin,DetailView):
    model = ToDoList
    template_name = 'notepad/notepad_detail.html'

    def get_context_data(self,**kwargs):
        context = super(ToDoDetailView,self).get_context_data(**kwargs)
        context['tasks_published'] = self.object.tasks.filter(status="published")
        #context['tasks_counted'] = self.object.tasks.filter(status="published").count()
        return context

urls.py(在记事本应用文件夹中)

from django.urls import path
from .views import ChangeTaskStatusView ,ToDoListeDelete,ToDoListUpdate, ToDoTrashDetailView,ToDoListView, ToDoDetailView, ToDoCreate, ToDoListCreate, ToDoListFirstCreate

app_name = "notepad"

urlpatterns = [
    path('add_formset/', ToDoListCreate.as_view(), name='add_formset'),
    path('add_todos/', ToDoListFirstCreate.as_view(), name='add_formsets'),
    path('add/', ToDoCreate.as_view(), name='add'),
    path('trash/<int:pk>', ToDoTrashDetailView.as_view(), name='todo_detail_trash'),
    path('<int:pk>', ToDoDetailView.as_view(), name='todo_detail'),
    path('update/<int:pk>', ToDoListUpdate.as_view(), name='todo_update'),
    path('delete/<int:pk>', ToDoListeDelete.as_view(), name='todo_delete'),
    path('', ToDoListView.as_view(), name='todo_list'),
    path('ajax_change_task_status/', ChangeTaskStatusView.as_view(), name='ajax_change_task_status'),
]

模板 (notepad_detail.html)

<!-- templates/books/book_detail.html -->
% extends 'base.html' %

% block title % object.headline % endblock title %

% block content %
<div>
    <h2> object.headline </h2> 
    <p>created by:  object.author , at  object.created </p>

    <ul class="nav justify-content-end">
            <li class="nav-item">
              <a class="nav-link active" href="% url 'notepad:todo_list' %">Back to List</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="% url 'notepad:todo_update' pk=object.id %">Update</a>
            </li>
            <li class="nav-item">
                    <a class="nav-link" href="% url 'notepad:todo_detail_trash' todolist.pk %">View finished tasks</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="% url 'notepad:todo_delete' pk=object.id %" tabindex="-1" aria-disabled="true">Delete</a>
            </li>
    </ul>
</div>


<div>
  <table class="table table-striped">
      <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">Tasks</th>
          <th scope="col">Total Tasks: tasks_published.count </th>
        </tr>
      </thead>
      <tbody>
          % for todo in tasks_published %
          <tr id="todo.id"> <th scope="row"> forloop.counter </th> <td> todo.todos </td> <td><button type="button" value="todo.id" onclick="changeStatus('todo.id');" class="btn btn-danger btn-sm">Trash</button></td> </tr>
          % endfor %
      </tbody>
  </table>
</div>
% endblock content %

base.html 必须将 javascript 代码放入 base.html 或扩展 javascript 块

% load static i18n compress %<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>% block title %tasks% endblock title %</title>

    <meta name="description" content="">
    <meta name="author" content="">

    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
    <![endif]-->

    <link rel="icon" href="% static 'images/favicons/favicon.ico' %">

    % block css %
    <!-- Latest compiled and minified Bootstrap CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <!-- Your stuff: Third-party CSS libraries go here -->
    % compress css %
    <!-- This file stores project-specific CSS -->

    <link href="% static 'css/project.css' %" rel="stylesheet">
    % endcompress %
    % endblock %

  </head>

  <body>

    <div class="mb-1">
      <nav class="navbar navbar-expand-md navbar-light bg-light">
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <a class="navbar-brand" href="% url 'home' %">tasks</a>

        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
              <a class="nav-link" href="% url 'home' %">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="% url 'about' %">About</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="% url 'notepad:todo_list' %">Notepads</a>
            </li>
            % if request.user.is_authenticated %
              <li class="nav-item">
                # URL provided by django-allauth/account/urls.py #
                <a class="nav-link" href="% url 'users:detail' request.user.username  %">% trans "My Profile" %</a>
              </li>
              <li class="nav-item">
                # URL provided by django-allauth/account/urls.py #
                <a class="nav-link" href="% url 'account_logout' %">% trans "Sign Out" %</a>
              </li>
            % else %
              <li class="nav-item">
                # URL provided by django-allauth/account/urls.py #
                <a id="sign-up-link" class="nav-link" href="% url 'account_signup' %">% trans "Sign Up" %</a>
              </li>
              <li class="nav-item">
                # URL provided by django-allauth/account/urls.py #
                <a id="log-in-link" class="nav-link" href="% url 'account_login' %">% trans "Sign In" %</a>
              </li>
            % endif %
          </ul>
        </div>
      </nav>

    </div>

    <div class="container">

      % if messages %
          % for message in messages %
              <div class="alert % if message.tags %alert- message.tags % endif %"> message <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>
          % endfor %
      % endif %

      % block content %
        <p>EveryCheese is the Ultimate Cheese Index! This text is inherited on other pages!</p>
      % endblock content %

    </div> <!-- /container -->

    % block modal %% endblock modal %

    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    % block javascript %
      <!-- Bootstrap JS and its dependencies-->
      <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

      <!-- Your stuff: Third-party javascript libraries go here -->
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
      <script type="text/javascript">
      function changeStatus(task)
        $.get( "% url 'notepad:ajax_change_task_status' %", task )
          .done(function( data ) 
             $('tr#'+task).remove();
          );
      
</script>

      <!-- place project specific Javascript in this file -->
      % compress js %
      <script src="% static 'js/project.js' %"></script>
      % endcompress %
    % endblock javascript %
  </body>
</html>

urls.py 全局文件

from django.conf import settings
from django.urls import include, path
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from django.views import defaults as default_views

urlpatterns = [
    path("", TemplateView.as_view(template_name="pages/home.html"), name="home"),
    path(
        "about/", TemplateView.as_view(template_name="pages/about.html"), name="about"
    ),
    # Django Admin, use % url 'admin:index' %
    path(settings.ADMIN_URL, admin.site.urls),
    # User management
    path("users/", include("tasks.users.urls", namespace="users")),
    path("accounts/", include("allauth.urls")),
    # Your stuff: custom urls includes go here
    path('todolists/', include('notepad.urls', namespace="notepad")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if settings.DEBUG:
    # This allows the error pages to be debugged during development, just visit
    # these url in browser to see how these error pages look like.
    urlpatterns += [
        path(
            "400/",
            default_views.bad_request,
            kwargs="exception": Exception("Bad Request!"),
        ),
        path(
            "403/",
            default_views.permission_denied,
            kwargs="exception": Exception("Permission Denied"),
        ),
        path(
            "404/",
            default_views.page_not_found,
            kwargs="exception": Exception("Page not Found"),
        ),
        path("500/", default_views.server_error),
    ]
    if "debug_toolbar" in settings.INSTALLED_APPS:
        import debug_toolbar

        urlpatterns = [path("__debug__/", include(debug_toolbar.urls))] + urlpatterns

这是我认为最有用的方法。我或多或少理解这一点,但我不知道谁可以更改代码以从任务外键对象中获取数据。

link to how i change a value in django on button click (不包括如何访问外键值/对象)

【问题讨论】:

您想将 ToDoList 的状态设置为垃圾箱吗?还是任务?? 显示你的 urls.py 文件 @گوروسینی 我想在 Detailview 中设置单个任务的状态。我希望在 ToDoList 的 Detailview 中的每个任务旁边都有一个按钮。当我单击按钮时,我想要单个任务的按钮,该单个任务的状态更改为垃圾 @PrabuReddy 编辑了 urls.py 【参考方案1】:

views.py

​​>

views.py

中添加 ChangeTaskStatusView
... other views ...

from .models import Tasks
from django.views import View
from django.http import HttpResponse

class ChangeTaskStatusView(View):
    def get(self, request):
        task = request.GET['task']
        Tasks.objects.filter(id = task).update(status='trash')
        return HttpResponse(task)

urls.py

​​>

urls.py,导入ChangeTaskStatusView查看并添加ajax_change_task_status url:

from .views import ChangeTaskStatusView

urlpatterns = [
    ... other urls ...
    path('ajax_change_task_status/', ChangeTaskStatusView.as_view(), name='ajax_change_task_status'),
]

模板

更新模板
... other htmls ...
<div>
    <table class="table table-striped">
        <thead>
          <tr>
            <th scope="col">#</th>
            <th scope="col">Tasks</th>
            <th scope="col">Total Tasks: <span id="task_count"> tasks_published.count </span></th>
          </tr>
        </thead>
        <tbody>
            % for todo in tasks_published %
            <tr id="todo.id"> <th scope="row"> forloop.counter </th> <td> todo.todos </td> <td><button type="button" value="todo.id" onclick="changeStatus('todo.id');" class="btn btn-danger btn-sm">Trash</button></td> </tr>
            % endfor %
        </tbody>
    </table>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript">
function changeStatus(task)
  $.get( "% url 'ajax_change_task_status' %", task )
    .done(function( data ) 
      $('tr#'+task).remove();
      $('#task_count').html($('#task_count').html()-1);
    );

</script>

【讨论】:

非常感谢您的帮助和努力。对此,我真的非常感激。我更改了更改/编辑的代码。当我单击“垃圾箱按钮”时它不起作用,也没有出现错误消息。 jquery 无法正确访问元素“tr# task”可能与某事有关? 我更改了更改/编辑的代码。我必须将 javascript 代码放在 base.html 中,并且 javascript 中的 url 是 ""% url 'notepad:ajax_change_task_status' %" ('namespace in global urls.py:name in app urls path')它工作。再次感谢您的帮助。我用正确的代码更新我的问题 @RobertF。让我知道你需要什么。 @sulhailvs 当我单击“垃圾箱按钮”时,您如何将 tasks_published.count 减少 1。它也需要用 jquery 来完成,对吗?否则我必须刷新页面才能查看详细视图中的实际任务数? @RobertF。我更新了答案见***.com/posts/60910817/revisions

以上是关于如何使用 Jquery 更改按钮单击时的 django 外键对象文本选择字段?的主要内容,如果未能解决你的问题,请参考以下文章

通过打字稿更改按钮单击时的svg颜色

java - 如何在java脚本或jquery中计算相同按钮单击3次和4次时的间隔时间

使用 JQuery 或 codeigniter 单击按钮时如何更改数据库表的值(设置为默认 false 的布尔列)

在 C# 中临时更改按钮单击时的按钮颜色

通过jQuery更改鼠标悬停时的动态按钮文本[复制]

使用 Node.js 和 Socket.IO 单击时的 jQuery 函数