How to show data when select and save

I am new in Django. In this project, select machine name and operation number, after select, we need to click on save button, after button click, data should display in the form of table, when button click only.

Please tell me where I got wrong. Please.

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})

template 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>

models.py:

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

    def __str__(self):
        return self.machine_name

when I clicked on save button, I got this error:

DoesNotExist at /save
Machine matching query does not exist.
Request Method: POST
Request URL:    http://localhost:8000/save
Django Version: 4.0
Exception Type: DoesNotExist
Exception Value:    
Machine matching query does not exist.
Exception Location: C:\Users\Manoj\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py, line 439, in get
Python Executable:  C:\Users\Manoj\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.5
Python Path:    
['C:\\Users\\Manoj\\Desktop\\sample\\mysite5',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32\\lib',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\Pythonwin']
Server time:    Thu, 13 Jan 2022 04:50:46 +0000

1 answer

  • answered 2022-01-13 05:30 ratchek

    You're trying to get machines before they're created. You need to use Machine.objects.get_or_create()

    
    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_or_create(machine_name=machine_name, operation_no=operation_no)
          machines = Machine.objects.all()
          return render(request,'usermaster/upload.html',{'machines':machines,'choiced_machine':choiced_machine})
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum