Django forms Reverse for '' with arguments '(7,)' and keyword arguments '{}' not found

后端 未结 2 586
悲&欢浪女
悲&欢浪女 2020-12-22 08:03

I have been running into this error for the past day and can\'t seem to solve it.

Reverse for \'\' with arguments \'(7,)\' and keyword arguments \'{}\' not          


        
2条回答
  •  北海茫月
    2020-12-22 08:49

    Your url tag should be {% url "drui" disease_id=disease.id %}, because you need to pass in the keyword argument.

    See the documentation for more information.

    As you never save the new entry, I think you are just using the form to display the ModelChoiceField, in that case you don't need a ModelForm:

    class DiseaseForm(forms.Form):
        disease = forms.ModelChoiceField(queryset=Disease.objects.all())
    

    That way, you avoid the commit=False part.

    You should always have an else for your if form.is_valid():

    from django.shortcuts import redirect
    
    def drui(request, disease_id):
       disease = get_object_or_404(Disease, pk=disease_id)
       ctx = {}
       ctx['disease'] = disease  
       ctx['indicatorInlineFormSet'] = IndicatorFormSet()
       ctx['diseaseForm'] = DiseaseForm()
    
       if request.method == "POST":
    
          diseaseForm = DiseaseForm(request.POST)
          indicatorInlineFormSet = IndicatorFormSet(request.POST, request.FILES)
    
          if diseaseForm.is_valid():
             return redirect('drui', disease_id=disease_id)
          else:
             # Form wasn't valid, return the same view to display the errors
             ctx['diseaseForm'] = diseaseForm
             ctx['indicatorInlineFormset'] = indicatorInlineFormset 
             return render(request, 'drui.html', ctx)
    
       else:
    
           return render(request, 'drui.html', ctx)
    

提交回复
热议问题