[Python] Why am I getting NoReverseMatch Error - Django

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm having a reverse error in Django when visiting another URL "comment_post_view"

    The error is coming from when I visit the comment_post_view page;

    I'm thinking maybe it's because of the username in url, but I don't know how to go on with it. How can I get it done?

    [​IMG]

    URL

    path('p/<username>/status/<post_id>/', comment_post_view, name='comment_post_view'),
    path('settings/archive-post/', archive_post_view, name='archive_post_view'),
    path('archive-post/<id>/', archive_view, name='archive_view'),

    VIEWS.PY

    @login_required
    def comment_post_view(request, username, post_id):
    page_title = "Post Comment"

    username_url = get_object_or_404(User, username=username)
    user_post = get_object_or_404(Post, pk=post_id, poster_profile=username_url)

    # All users following posts
    user_profile = request.user.profile
    posts = Post.objects.filter(
    Q(id=user_post.id)
    ).order_by("?").select_related('poster_profile', 'poster_profile__profile').distinct()

    post_data = []
    for post in posts:
    poster_profile = post.poster_profile.profile
    mutual_followers_qs = user_profile.following.filter(
    id__in=poster_profile.following.values_list('id', flat=True)
    )
    post_data.append({
    'post': post,
    'mutual_followers': mutual_followers_qs[:3], # You can select any number of followers here
    'mutual_count': mutual_followers_qs[2:].count()
    })


    ............................

    @login_required
    def archive_post_view(request):
    page_title = "Archive Post"

    posts = Post.objects.filter(
    Q(is_hide=request.user)
    ).select_related('poster_profile', 'poster_profile__profile').distinct()

    post_data = []
    for post in posts:
    poster_profile = post.poster_profile.profile
    post_data.append({
    'post': post,
    })


    ..........................

    @login_required
    def archive_view(request, id):
    post = get_object_or_404(Post, id=id)
    if post.is_hide.filter(id=request.user.id).exists():
    post.is_hide.remove(request.user)
    else:
    post.is_hide.add(request.user)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))


    TEMPLATE archive_post_view.html

    {% for item in post_data %}
    {% if request.user == item.post.poster_profile %}
    <a href="{% url 'site:archive_view' item.post.id %}">Archive post</a> # Current user
    {% else %}
    # Error pointing to this a href
    <a href="{% url 'site:archive_view' item.post.id %}">Archive post</a> # Other user
    {% endif %}
    {% endfor %}

    Continue reading...

Compartilhe esta Página