Search code examples
pythondjangopython-3.xdjango-2.1

NoReverseMatch error for combined form and model_formset


I have been trying to create a for that combines parent and child models using model_formsets by following use case three in this tutorial.

Models

class Shoppinglist(models.Model):
    name = models.CharField(max_length=50)
    description = models.TextField(max_length=2000)
    created = models.DateField(auto_now_add=True) 
    created_by = models.ForeignKey(User, related_name='lists', on_delete=models.CASCADE)
    last_updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

class Item(models.Model):
    name = models.CharField(max_length=80, unique=True)
    amount = models.IntegerField(default=1)
    shoppinglist = models.ForeignKey(Shoppinglist, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

URLs

urlpatterns = [
   url(r'^shoppinglists/(?P<pk>\d+)/$', views.shoppinglist_list, name='shoppinglist_list'),
   url(r'^shoppinglists/new/$', views.create_shoppinglist_with_items, name='shoppinglist_new'),
]

Forms

class ShoppingListForm(forms.ModelForm):
    description = forms.CharField(
        widget=forms.Textarea(
            attrs={'rows': 5, 'placeholder': 'Tell us about your list?'}
        ), 
        max_length=4000,
        help_text='The max length of the text is 4000 characters.'
    )

    class Meta:
        model = Shoppinglist
        fields = ['name', 'description']

ItemFormset = modelformset_factory(
    Item,
    fields=('name', 'amount'),
    extra=1,
    widgets={
        'name': forms.TextInput(
            attrs={
                'class': 'form-control',
                'placeholder': 'Enter Item Name here'
            }
        )
    },
    can_delete=True
    
)

View

@login_required
def create_shoppinglist_with_items(request):
    template_name = 'list_with_items.html'
    if request.method == 'GET':
        listform = ShoppinglistForm(request.GET or None)
        formset = ItemFormset(queryset=Item.objects.none())
    elif request.method == 'POST':
        listform = ShoppinglistForm(request.POST)
        formset = ItemFormset(request.POST)
        if listform.is_valid() and formset.is_valid():
            shoppinglist = listform.save(commit=False)
            shoppinglist.created_by = request.user
            shoppinglist = listform.save()
            for form in formset:               
                item = form.save(commit=False)
                item.shoppinglist = shoppinglist
                item.save()
            return redirect('shoppinglist_list', pk=shoppinglist.pk)
    return render(request, template_name, {
        'listform': listform,
        'formset': formset,
    })

Template

{% block content %}
<form method="post">
    {% csrf_token %}

    <label>List Name</label>
    {{ listform.name }}
    {% if listform.first_name.errors %}
        {% for error in listform.first_name.errors %}
            {{ error|escape }}
        {% endfor %}
    {% endif %}

    <label>Description</label>
    {{ listform.description }}
    {% if listform.description.errors %}
        {% for error in listform.description.errors %}
            {{ error|escape }}
        {% endfor %}
    {% endif %}

    {{ formset.management_form }}

    {% for form in formset %}
        <div class="item-formset">
            {{ form.amount }}
            {% if form.amount.errors %}
                {% for error in form.amount.errors %}
                    {{ error|escape }}
                {% endfor %}
            {% endif %}

            {{ form.name }}
            {% if form.name.errors %}
                {% for error in form.name.errors %}
                    {{ error|escape }}
                {% endfor %}
            {% endif %}
        </div>
    {% endfor %}

    {% if formset.non_form_errors %}
        {% for error in formset.non_form_errors %}
            {{ error|escape }}
        {% endfor %}
    {% endif %}

    <div class="row spacer">       
            <button type="submit" class="btn btn-block btn-primary">Create</button>
    </div>
</form>
{% endblock %}

{% block extra_js %}
<script>
  $('.item-formset').formset({
      addText: 'add item',
      deleteText: 'remove'
  });
</script>
{% endblock %}

I have modified the code so that the user would be taken to the newly created parent model but instead, when I go to the page I get a NoReverseMatch error.

Reverse for 'shoppinglist_list' with arguments '('',)' not found. 1 pattern(s) tried: ['shoppinglists/(?P<pk>\\d+)/$']


Solution

  • Just to close this ticket there was a typo in the template, which I have corrected, that was causing this issue. So the above code does now work.