I am working on a site that will be used to clean up inactive Tableau workbooks. Logging into this site will allow my users to see their old workbooks and decide which ones to keep.
This is going to be accomplished by taking some simple text input from an HTML page, K for keep | D for delete.
The response from the user will then be stored as a Python variable that will go into an if then statement. The if then statement will basically update each row in SQL, adding either K or D to a column called “Marked_for_Deletion”.
From there, a stored procedure will run, check that column, and delete all things marked with a D.
Is this feasible? If so, how would I go about pulling that input and making sure it gets added to the right column/row? If not, can you offer any suggestions on a substitute method I can use?
Thanks!
Edit: Here is the code for my table.
<table class="blueTable"> <thead> <tr> <th>Workbook Name</th> <th>Deletion Deadline</th> <th>Keep or Delete?</th> </tr> </thead> <tbody> {% for book in Workbooks %} <tr> <td>{{ book.name }}</td> <td>{{ book.owner_name }}</td> <td> <label class="container" style="margin-bottom: 25px"> <input type="text" placeholder="(Enter K for Keep, D for Delete)"> </label> </td> </tr> {% endfor %} </tbody> </table> <form method="post"> {% csrf_token %} <button type="submit" name="run_script">Submit</button> </form> </body> </html>
I want to be able to pull the input from the last td tag and store it with the submit button below there.
Advertisement
Answer
I’m kind of late, but I hope it helps you out.
urls.py
from django.urls import path from workbook_app import views app_name = 'workbook_app' urlpatterns = [ path('books/', views.BookListView.as_view(), name='books'), path('keep_or_delete/<int:pk>/', views.KeepOrDeleteView.as_view(), name='keep_or_delete'), ]
models.py
from django.db import models class Book(models.Model): name = models.CharField(max_length=250) owner_name = models.CharField(max_length=250) marked_for_deletion = models.BooleanField(default=False)
views.py
from django.views.generic import ListView from workbook_app.models import Book from django.http import HttpResponse, HttpResponseRedirect from django.views import View from django.urls import reverse class BookListView(ListView): template_name = 'workbook_app/books.html' def get_queryset(self): return Book.objects.all() class KeepOrDeleteView(View): def post(self, request, pk): book = Book.objects.get(pk=pk) print(book, book.marked_for_deletion, not book.marked_for_deletion) book.marked_for_deletion = not book.marked_for_deletion book.save() url = reverse('workbook_app:books') return HttpResponseRedirect(url)
books.html
<div class="container"> <h2>Books</h2> <table class="table"> <tr> <th>Workbook Name</th> <th>Deletion Deadline</th> <th>Keep or Delete?</th> </tr> {% for book in object_list %} <tr> <td>{{book.name}}</td> <td>{{book.owner_name}}</td> <td> <form action="{% url 'workbook_app:keep_or_delete' pk=book.pk %}" method="POST"> {% csrf_token %} <button type="submit" class="btn btn-{%if book.marked_for_deletion %}primary{% else%}danger{% endif %}">{%if book.marked_for_deletion %}Keep{% else%}Delete{% endif %}</button> </form> </td> </tr> {%endfor%} </table>
P.S. I’m not handling exceptions, messages, etc. You’re just gonna have to figure it out yourself or open a new question.