get_absolute_url is not working--gives this error NoReverseMatch error

I get this error when trying to user get_absolute_url–what i am doing wrong? I do pass the slug, I have it in the url and the fun. you can see how i pass everywhere it needs to be passed and i still get this error.

NoReverseMatch at /mega_archive/books/

Reverse for 'book' with arguments '('',)' not found. 1 pattern(s) tried: ['mega_archive/book/(?P<book>[-a-zA-Z0-9_]+)/\\Z']

views.py

def books(request):
    books = Book.objects.all()
    return render(request, 'mega_archive/books.html',{
                'books':books
        })


def book(request, book):
    book = get_object_or_404(Book, slug=book)
    return render(request, 'mega_archive/book_details.html', {
                'book':book
        })

urls.py

    path('books/', views.books, name='books'),
    path('book/<slug:book>/', views.book, name='book'),

models.py

def get_absolute_url(self):
        return reverse('mega_archive:book', args=[self.slug])

    def __str__(self):
        return self.title


html

{% for book in books %}

    <a href="{{book.get_absolute_url}}">{{book}}</a><br>
{% endfor %}

It works and then after awhile it stops working, strange

This description implies that you have one or more Book objects with a null or blank slug field.

I dumped by deleting my migrations and the database by deleting the db.sqlite3 file–did migrations again a new and it worked. Mysterious thing this Django

1 Like

This is likely going to be a problem again until you make that slug field removes the possibility of an empty string. You can use the MinLengthValidator to help.

Hello Ken, are you familiar with django_comments_xtd? I’m currently working on it and the commenting system seems to work fine, however–when I reply to a comment, the reply is not threading, in other words, it does not nest under the comment properly.

This is what I’m getting:

This is how its supposed to be:

I’ve been playing with the docs and the settings in my project and I still can’t figure out why the reply is not positioned properly.

Can you assist?

Here is my settings.py

SITE_ID = 1;
COMMENTS_APP = 'django_comments_xtd'
COMMENTS_XTD_MAX_THREAD_LEVEL = 2  # site wide default
COMMENTS_XTD_MAX_THREAD_LEVEL_BY_APP_MODEL = {
    # Objects of the app blog, model post, can be nested
    # up to thread level 1.
        'blog.post_details': 1,
}

my html.file with the comments:

{% load i18n %}
{% load comments %}
{% load comments_xtd %}

{% for item in comments %}
<div class="media">
  <a name="c{{ item.comment.id }}"></a>
  <div class="mr-3">{{ item.comment.user_email|xtd_comment_gravatar }}</div>
  <div class="media-body">
    <div class="comment pb-3">
      <h6 class="mb-1 small d-flex">
        <div class="mr-auto">{{ item.comment.submit_date }}&nbsp;-&nbsp;{% if item.comment.url and not item.comment.is_removed %}<a href="{{ item.comment.url }}" target="_new">{% endif %}{{ item.comment.name }}{% if item.comment.url %}</a>{% endif %}{% if item.comment.user and item.comment.user|has_permission:"django_comments.can_moderate" %}&nbsp;<span class="badge badge-secondary">{% trans "moderator" %}</span>{% endif %}&nbsp;&nbsp;<a class="permalink" title="{% trans 'comment permalink' %}" href="{% get_comment_permalink item.comment %}">¶</a></div>
        <span>
          {% if not item.comment.is_removed %}
            {% if perms.comments.can_moderate %}
              {% if item.flagged_count %}
                <span class="badge badge-danger" title="{% blocktrans count counter=item.flagged_count %}A user has flagged this comment as inappropriate.{% plural %}{{ counter }} users have flagged this comment as inappropriate.{% endblocktrans %}">{{ item.flagged_count }}</span>
              {% endif %}
            {% endif %}
            {% if allow_flagging and request.user in item.flagged %}
              <i class="fas fa-flag text-danger" title="{% trans 'comment flagged' %}"></i>
            {% elif allow_flagging %}
              <a class="mutedlink"
                 href="{% url 'comments-flag' item.comment.pk %}">
                <i class="fas fa-flag" title="{% trans 'flag comment' %}"></i>
              </a>
            {% endif %}
            {% if perms.comments.can_moderate %}
              <a class="mutedlink"
                 href="{% url 'comments-delete' item.comment.pk %}"><i class="fas fa-trash-alt" title="{% trans 'remove comment' %}"></i></a>
            {% endif %}
          {% endif %}
        </span>
      </h6>
      {% if item.comment.is_removed %}
        <p class="text-muted{% if not allow_feedback and not item.comment.allow_thread %} pb-3{% endif %}"><em>{% trans "This comment has been removed." %}</em></p>
      {% else %}
        <div class="content{% if not allow_feedback and not item.comment.allow_thread %} pb-3{% endif %}">
        {% include "includes/django_comments_xtd/comment_content.html" with content=item.comment.comment %}
        </div>
        {% if allow_feedback %}
          {% include "includes/django_comments_xtd/user_feedback.html" %}
        {% endif %}
        {% if item.comment.allow_thread and not item.comment.is_removed %}
          {% if allow_feedback %}&nbsp;&nbsp;<span class="text-muted">&bull;</span>&nbsp;&nbsp;{% endif %}<a class="small mutedlink" href="{{ item.comment.get_reply_url }}">{% trans "Reply" %}</a>
        {% endif %}
      {% endif %}
    </div>
    {% if not item.comment.is_removed and item.children %}
      {% render_xtdcomment_tree with comments=item.children %}
    {% endif %}
  </div>
</div>
{% endfor %}

Thank you

No, I’m not familiar with that package.

But generally speaking, that sort of nested representation means that you would need to have a model that maintains a relationship between a comment and its parent comment. You would then use a recursive reference in your template to iterate over the “child comments” for each comment - where each child comment calls the same template for its child comments.

me same problme
It was fixed without me doing anything :slightly_smiling_face: