简体   繁体   中英

Django TypeError got an unexpected keyword argument 'opcao_id'

Okay so I'm trying to delete a "opcao" related to a "questao" In my html I have this button:

<a href="{% url 'votacao:apagaopcao' questao.id %}">
    <button> Apagar Opção </button>
</a>

which leads to this url:

url(r'^(?P<opcao_id>[0-9]+)/apagaopcao/$', views.apagaopcao, name="apagaopcao"),

And this is that view:

def apagaopcao(request, opcao_id, questao_id):
questao = get_object_or_404(Questao, pk=questao_id)
try:
    opcao_seleccionada = questao.opcao_set.get(pk=request.POST['opcao'])
except (KeyError, Opcao.DoesNotExist):
    # Apresenta de novo o form para votar
    return render(request, 'votacao/detalhe.html', {'questao': questao, 'error_message': "Não escolheu uma opção",})
else:
    opcao_seleccionada.delete()
    return render(request, 'votacao/detalhe.html', {'questao': questao})

And it throws me this exception:

Exception Type: TypeError
Exception Value: apagaopcao() missing 1 required positional argument: 'questao_id'

I'm just starting to try to figure django out so what am i missing here? Thanks!!

You only defined one named argument opcao_id in url definition, but you have 2 parameters in your views opcao_id and questao_id , which of course wouldn't work. You should either add questao_id in your url definition, or remove it from your views function.

Check django doc on how url parameters work with views function .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM