在Django中,我在从HTML获取id时遇到了麻烦;错误为:(字段"id"应为数字,但得到的却是数字)

bhmjp9jg  于 2023-02-10  发布在  Go
关注(0)|答案(1)|浏览(169)

当用户选择翻译者的名字时,我想通过id访问Django中选择的翻译者,并将信息存储在数据库的约会表中,但当他试图匹配id时出现了错误,错误是:字段"id"需要一个数字,但得到的却是""。
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代码:我只显示具有相同语言的管理器,以提示用户发送新消息。

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

在 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')

错误来自:

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

你们知道我该怎么解决吗?
你们知道我该怎么解决吗?

oxiaedzo

oxiaedzo1#

而不是这个:
<form method="POST" action="/waite" >
试试这个:

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

Django使用的是Jinja模板,这可能是你没有得到id的原因。

相关问题