Skip to content

How can I take input from a text field in a Django page then update a SQL table with the response?

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?


Edit: Here is the code for my table.

 <table class="blueTable">
                <th>Workbook Name</th>
                <th>Deletion Deadline</th>
                <th>Keep or Delete?</th>
            {% for book in Workbooks %}
                <td>{{ }}</td>
                <td>{{ book.owner_name }}</td>
                    <label class="container" style="margin-bottom: 25px">
                        <input type="text" placeholder="(Enter K for Keep, D for Delete)">
            {% endfor %}
    <form method="post">
        {% csrf_token %}
        <button type="submit" name="run_script">Submit</button>


I want to be able to pull the input from the last td tag and store it with the submit button below there.



I’m kind of late, but I hope it helps you out.

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'),

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)

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

        url = reverse('workbook_app:books')
        return HttpResponseRedirect(url)


<div class="container">
<table class="table">
    <th>Workbook Name</th>
    <th>Deletion Deadline</th>
    <th>Keep or Delete?</th>
  {% for book in object_list %}
      <form action="{% url 'workbook_app:keep_or_delete' %}" 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>

P.S. I’m not handling exceptions, messages, etc. You’re just gonna have to figure it out yourself or open a new question.

1 People found this is helpful