PacktPublishing/Web-Development-with-Django

django.urls.exceptions.NoReverseMatch: Reverse for 'publisher_edit' with arguments '(None,)' not found. 1 pattern(s) tried: ['books/publishers/(?P<p_id>[0-9]+)/$']

melission opened this issue · 1 comments

Hi,
In Chapter07/Exercise7.03/bookr/reviews/views.py I met a problem, namely I got an exception as updated_publisher doesn't have an id ('pk' in this book). My code:
views.py

 if request.method == 'POST':
        form = PublisherForm(request.POST, instance=publisher)
        if form.is_valid():
            updated_publisher = form.save()
            if publisher is None:
                messages.success(request, f'Publisher {updated_publisher} was created.')
            else:
                messages.success(request, f'Publisher {updated_publisher} was updated.')  
          
            return redirect("publisher_edit", updated_publisher.id)

forms.py

class PublisherForm(forms.ModelForm):
    class Meta:
        model = Publisher
        fields = '__all__'

models.py

class Publisher(models.Model):
    id = models.IntegerField(primary_key=True, editable=False)
    name = models.CharField(max_length=64)
    website = models.URLField(help_text="Publisher's website", blank=True)

    def __str__(self):
        return self.name

Yet, if I get a database object and return redirect based on it (the last line of the code), everything works as was intended.

if request.method == 'POST':
        form = PublisherForm(request.POST, instance=publisher)
        if form.is_valid():
            updated_publisher = form.save()
            # get a database obj
            pub = Publisher.objects.get(name=updated_publisher.name)
            if publisher is None:
                messages.success(request, f'Publisher {updated_publisher} was created.')
            else:
                messages.success(request, f'Publisher {updated_publisher} was updated.')  
            # pub - a database obj
            return redirect("publisher_edit", pub.id)

Though there is another problem. If I have more than one Publisher with the same name and try Publisher.objects.get(), I get an error as .get() can retrieve only one object.

I don't know whether the authors are reading issues, yet if they are, it would be nice to get a piece of advice or an explanation how I can get an id value ('pk' in this book).

Thanks!

Check your Publisher model. In Django, you don't need to specify the id field as it's created automatically. It should be like this.

class Publisher(models.Model):
    """A company that publishes books."""
    name = models.CharField(max_length=50,
                            help_text="The name of the Publisher.")
    website = models.URLField(help_text="The Publisher's website.")
    email = models.EmailField(help_text="The Publisher's email address.")

    def __str__(self):
        return self.name

Everything else looks fine. You can check out the final bookr code for Chapter 7 here: https://github.com/PacktPublishing/Web-Development-with-Django/tree/master/Chapter07/final/bookr and compare it with what you have.

Though there is another problem. If I have more than one Publisher with the same name and try Publisher.objects.get(), I get an error as .get() can retrieve only one object.

That's to be expected, the get() method is designed to retrieve exactly one object.