0

I have built a small web application that will get the data from a HTML table and dump it into sqlite database. When I use the POST method to capture a particular cell value, it returns none. Could someone please help me how to get the data from the table I created below in table.html ? Initially I am just trying to print the value of the element "name", later on I will add it to DB. I want the output as ["name1","name2"]

models.py

from django.db import models

class people(models.Model):
    Company = models.TextField()
    Contact = models.TextField()
    Country = models.TextField()

    def __str__(self):
        return self.Contact

urls.py

from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [
    path('', views.getvalue, name='myapp'),
]

views.py

from django.shortcuts import render

def home(request):
    return render(request, 'myapp/table.html')

def getvalue(request):

    if request.method == "POST":
        value = request.POST.get("cellvalue")
        print("The values is", value, request.method)
        return render(request, 'myapp/table.html')

table.html

<html>
<head>
</head>
<body>

<form role="form" action="getvalue", method="post">{% csrf_token %}
  <input type="submit" value="Update DB" />
  <table>
    <tr>
      <td name='cellvalue'>name1</td>
      <td name='cellvalue'>name2</td>
    </tr>
  </table>
</form>

</body>
</html>
  • Perhaps try specifying the form action? https://stackoverflow.com/questions/9401521/is-action-really-required-on-forms – Rekamanon Dec 13 '19 at 21:05
  • Also wouldn't hurt to verify that request.method == 'POST' – Rekamanon Dec 13 '19 at 21:07
  • Does this answer your question? [django - what goes into the form action parameter when view requires a parameter?](https://stackoverflow.com/questions/5467192/django-what-goes-into-the-form-action-parameter-when-view-requires-a-parameter) – Rekamanon Dec 13 '19 at 21:21
  • @Rekamanon: 1) I have specified the form action. 2) I have verified that the method is post 3) I The link does not really say how to get get value from table elements. Thanks for your help in advance. Moreover, this technique works if I queried a the value from a simple entry box. I just does not work for tables. Could you please check in on this and confirm ? –  Dec 14 '19 at 04:57

2 Answers2

0

well i am not really a django guy but are you sure your template is posting any value?

def getvalue(request):
    value = request.POST.get("R1")
    print("The R1 values is", value)
    return render(request, 'myapp/table.html')

on the first call there wont be any post variable at all so if you like to handle this you might be better of with a FormView.

Update on some stuff (might not be the best way because I am no django pro)

so i would go for a FormView that is providing some functionality out of the box. I only got an exmaple for an AuthenticationForm but it might start you off here

so simple view could look like that

class UserLoginView(FormView):
    form_class = AuthenticationForm #UserLoginForm
    template_name = 'yourApp/registration/login.html'
    success_url = reverse_lazy('users_list') 

and the template for the form could be something like this

<!-- templates/registration/login.html -->
{% load staticfiles %}

<link rel="stylesheet" type="text/css" href="{% static 'yourApp/css/style.css' %}">

<h2>Login</h2>
<form method="post" >
  {% csrf_token %}
  {{ form.as_p }}
  <input type="submit"/>
</form>

so the magic is done by django here since we only tell the template to post stuff with post and the form magic is provided because django knows we use a authentication form. So to do a lot more usefull stuff you might need to read up a little and build out your template accordingly. That said you need to tell your form where to send the post data!!

so does this line really POST data to your getvalue methode???

<form role="form" action="getvalue", method="post">

if you dont know why not do a little print debugging in your method?

def getvalue(request):
    print(dir(request))
    if request.method == "POST":
        value = request.POST.get("cellvalue")
        print("The values is", value, request.method)
        return render(request, 'myapp/table.html')

you should see a lot of stuff in your console now and figure out what finegrained stuff you really want to know.

Update on your Code

with this kind of form

<form role="form" action="getvalue", method="post">{% csrf_token %}

  <table class = "table1">
    <input type="submit" value="Update DB"/>
    <tr>
      <td><input class = splinput type="label" name = "cellvalue" value=""/></td>
      <td><input class = splinput type="label" name = "cellvalue" value=""/></td>
      <td><input class = splinput type="label" name = "cellvalue" value=""/></td>
    </tr>

</form>

you need to make it clear for the backend wht to do with what.so your line

becomes something like

      <td><input class = splinput type="text" name = "give_me_some_unique_name" value="and_pass_a_value"/></td>

to find out what input types are there you might want to go here

https://www.w3schools.com/html/html_form_input_types.asp

and read up on it

after that your view should get something you can handle the request.POST object. Here your input name should be the key and value of course value.

Markus Rosjat
  • 146
  • 2
  • 8
  • I have modified the code slightly could you please tell me if there is still anything wrong with the code ? –  Dec 14 '19 at 04:59
  • like i said i dont do much with django so i cant really debug much but i try to share my findings when i started out playing with django. I will put some update in my post on how i would approach it. And bear in mind i am also a nocive here – Markus Rosjat Dec 14 '19 at 12:03
0

I modified the HTML file slightly and I got the expected results. It appears like I needed to insert a input within every "td element" and then query its value. Please find the code below:

<html>
<body>

<style>
.splinput {
  border: none;
  background: transparent;
  height: 100%;
  width: 100%;
}

table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 30%;
  table-layout:fixed;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 0px;
  height: 30
}

</style>

<form role="form" action="getvalue", method="post">{% csrf_token %}

  <table class = "table1">
    <input type="submit" value="Update DB"/>
    <tr>
      <td><input class = splinput type="text" name = "cellvalue" value=""/></td>
      <td><input class = splinput type="text" name = "cellvalue" value=""/></td>
      <td><input class = splinput type="text" name = "cellvalue" value=""/></td>
    </tr>

</form>

</body>
</html>
  • what do you want to accoplish ? do you want to type in values into the input? if so you need a texbox forthat. If not you need to get things distinguishable. so you name all imput cellvalue and set the value to an empty string thats not distinguishable and label is no input type! – Markus Rosjat Dec 14 '19 at 14:18
  • My objective was very simple. I have a table and I was unable to retrieve the values of the cell elements from the table using the POST method. The cells are editable, i made it editable using the command "contenteditable = true" option. My question is when I write something like this name1 why was I unable to get the text content within the cell using post ? –  Dec 14 '19 at 15:08
  • when you read the django docs it tells you POST is a dict like object and as far as i saw you can use a get method there. Soif you do POST.get("a_name") it should give you the value of the POST variable a_name. Then you need to make sure a_name gets a value in your form. So first of all try the print debugging approach to see if you get values from your inputs in the POST object. And you might need to think what has precedence here, the value="" or the string between the input tags but thats also easy to find out, put a value="foo" in and debug your POST Object... – Markus Rosjat Dec 14 '19 at 15:23
  • I tried doing that and it returns none, when I didnt have inputs within td's –  Dec 15 '19 at 04:26
  • again label is not an imput field, just try something simple first just a form with an imput for text and one to submit it. maybe try to define the action to your html page not the view and see what you end up with and take it from there. I can't really debug for you and i can't teach you html thats a tasks you have to do. I pointed out stuff you need to examine and the part to do that is on your site. – Markus Rosjat Dec 15 '19 at 10:10
  • See, I do not want to use to get POST value. I wanna use –  Dec 15 '19 at 17:42
  • since td is not really passing data into the form i dont think its possible. with the input approach you can pass data to the form. I dont know if you could inspect the HTML in the request and try to figure out whats is between a open/closed td tag. – Markus Rosjat Dec 15 '19 at 18:27
  • Oh Okay thats what I wanted to confirm –  Dec 15 '19 at 19:08
  • yeah sorry but like i said i dont do much with django but take a look at the input types of html you might find one that is suited for your needs. Or just dig in the django docs it might gives you a better solution – Markus Rosjat Dec 15 '19 at 21:05