Home > Blockchain >  displaying None instead of data in the form of table
displaying None instead of data in the form of table

Time:01-13

Here, select machine name and operation number, after select and save, It is displaying none instead of data like machine name and operation number in the form of table. Please help me out to solve this. I am new in Django.

urls.py:

urlpatterns = [
    path('upload/',views.upload,name='upload'),
    path('save',views.save_machine,name='save_machine')
]

views.py:

def upload(request):
    machines = Machine.objects.all()
    return render(request,'usermaster/upload.html',{'machines':machines})

def save_machine(request):
   if request.method == "POST":
      machine_name = request.POST.get('machine_name', '')
      operation_no = request.POST.get('operation_no')
      choiced_machine = Machine.objects.get(machine_name=machine_name, operation_no=operation_no)
      machines = Machine.objects.all()
      return render(request,'usermaster/upload.html',{'machines':machines,'choiced_machine':choiced_machine})

models.py:

class Machine(models.Model):
    machine_name = models.CharField(max_length=200)
    operation_no = models.IntegerField(null=False)

    def __str__(self):
        return self.machine_name

upload.html:

<form action="{% url 'save_machine' %}" method="post">
    {% csrf_token %}
       <select>
           <option>Select Machine Name</option>
           {% for machine in machines %}
           <option name="machine_name">{{ machine.machine_name }}</option>
           {% endfor %}
       </select>
       <br>
       <br>
       
       <select>
           <option>Select Operation Number</option>
           {% for machine in machines %}
           <option name="operation_no">{{ machine.operation_no }}</option>
           {% endfor %}
           </select>
       <br>
       <br>
       <br>
       <input type="submit" value="Save">
   </form>

<tr>
    <td>{{choiced_machine.machine_name}}</td>
    <td>{{choiced_machine.operation_no}}</td>
</tr>

CodePudding user response:

try this

<form action="{% url 'save_machine' %}" method="post">
    {% csrf_token %}
       <label for="machinename">Select Machine Name:</label>
       <select name="machinename" id="machinename">
           
           {% for machine in machines %}
           <option value="{{ machine.machine_name }}">{{ machine.machine_name }}</option>
           {% endfor %}
       </select>
       <br>
       <br>
       <label for="operationname">Select Operation Number:</label>
       <select id="operationname" name="operationname">
           
           {% for machine in machines %}
           <option value="{{ machine.operation_no }}">{{ machine.operation_no }}</option>
           {% endfor %}
           </select>
       <br>
       <br>
       <br>
       <input type="submit" value="Save">
   </form>

<tr>
    <td>{{choiced_machine.machine_name}}</td>
    <td>{{choiced_machine.operation_no}}</td>
</tr>

change your view like this.

def save_machine(request):
   if request.method == "POST":
      machine_name = request.POST.get('machinename', '')
      operation_no = request.POST.get('operationname','')
      choiced_machine = Machine.objects.get(machine_name=machine_name, operation_no=operation_no)
      machines = Machine.objects.all()
      return render(request,'usermaster/upload.html',{'machines':machines,'choiced_machine':choiced_machine})

CodePudding user response:

Try to edit your form like this below.Currenlty you are not passing any value so that internal server error occurs and you are not getting any response from the server so that None error is showing

 <select name="machine_name">
    <option>Select Machine Name</option>
    {% for machine in machines %}
    <option value="{{ machine.machine_name }}">{{ machine.machine_name }}</option>
    {% endfor %}
</select>

<select name="operation_no">
    <option>Select Operation Number</option>
    {% for machine in machines %}
    <option value="{{machine.operation_no}}">{{ machine.operation_no }}</option>
    {% endfor %}
</select>
  •  Tags:  
  • Related