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

How to change djangos foreign key objects textchoicefield on button click with Jquery?

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

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

我想使用 ajax 并尝试理解(我是新手)。有人可以 help/guide 支持我吗

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

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

(不包括如何访问外键 values/objects)

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_statusurl:

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>