In Django, I'm having trouble getting the id from HTML; the error is: ( Field "id" expected a number but got )

When the user selects the translator name, I want to access the selected translator in Django by the id and store the information in an appointment table in the database, but there is an error when he tries to mach the id the error is: Field 'id' expected a number but got ''.

HtML code:

 <section style="margin-top: 10%;">       
    <form method="POST" action="/waite" >
        {% csrf_token %} 
        <select id="translator" name="translator">
        {% for i in pro %}
             <option value="{{ i.id }}"> {{ i.name }}</option>
        {% endfor %}
      </select>
      <button type="submit">Send request</button>  

    </form>
</section>

in django view.py:

def waitePage(request):
        if request.method == 'POST':
            id = request.POST.get("translator")
            translator = Manager.objects.get(user_id=id)
            translatorName= translator.name #get the name of the translator
            translatorID= translator.id #get the id of the translator
        

            current_user  = request.user #to get the user
            current_userId = current_user.id #for storing user id
            customer = Customer.objects.get(id=current_userId) #to get the info of translator
            customerID=customer.id
            customerName=customer.name

            appointment = Appointment.objects.create(
            customerName=customerName,
            customerID=customerID, translatorID=translatorID, accepted=False,)
            appointment.save()
            return render(request,'waitePage.html')

The error comes from:

   translator = Manager.objects.get(user_id=id) 

Do you guys have any idea how I can solve it?

Do you guys have any idea how I can solve it?

Instead of this:

<form method="POST" action="/waite" >

try this one:

<form method="POST" action="{% url 'waitePage' %}">

Django use Jinja templating, that may be the reason why you don't get id.

Back to Top