0

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 ''.

view.py:  where I took the user's chosen language and did a filler for the manager.

def customerHomePage(request):
    if request.method == 'POST':
        search_id = request.POST.get('textfield', None)
        try:
            pro = Manager.objects.filter(Second_Language = search_id)
            x={'pro': pro}    
            return render(request,'representTranslator.html', x)#for show the customer Home page 
        except pro.DoesNotExist:
            return HttpResponse("no such user")  
    else:
        return render(request, 'customerHome.html')

HtML code: I displayed only the manager with the same chosen language to prompt the user to send a new message.

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

4
  • There was no item named "translator" in the POST data. Feb 7 at 19:53
  • It may be because request.POST.get("translator") is a string. Try changing it to an integer via int()
    – EE2021
    Feb 7 at 19:55
  • Show us the view code that renders the html template. Specifically, how the variable pro is created and passed into the template. Feb 7 at 20:05
  • ' if request.method == 'POST': search_id = request.POST.get('textfield', None) try: pro = Manager.objects.filter(Second_Language = search_id) x={'pro': pro} return render(request,'representTranslator.html', x)#for show the customer Home page ' I used it to display the manager that has the same language the user selected, going with a for loop over them to display them.
    – reema
    Feb 7 at 21:05

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.