Skip to main content
Asked
Viewed 10k times
2

I am following django tutorial by @CoreyMSchafer. I got error while practicing i can't find solution to it.

According to my understanding its problem with reversing of url. but can't find out what is wrong

Error: NoReverseMatch at /

Reverse for 'user-posts' with arguments '('',)' not found. 1 pattern(s) tried: ['user/(?P[^/]+)$']

For some reason error is in head of base.html where I'm linking bootstrap.

I also tried removing that link then its giving same error but at line 0 of base.html

views.py:

class UserPostListView(ListView):
    model = Post
    context_object_name = 'posts'
    template_name = 'blog/user_posts.html'

    paginate_by = 5

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Post.objects.all().filter(author= user).order_by('-date_posted')

urls.py file:

from django.urls import path, include
from .views import PostListView, PostDetailView, PostCreateView, PostUpdateView, PostDeletelView, UserPostListView
from . import views

urlpatterns = [
    path('',                        PostListView.as_view(),         name='blog-home'),
    path('user/<str:username>',    UserPostListView.as_view(),     name='user-posts'),
    path('post/<int:pk>/',          PostDetailView.as_view(),       name='post-detail'),
    path('post/new/',               PostCreateView.as_view(),       name='post-create'),
    path('post/<int:pk>/update/',   PostUpdateView.as_view(),       name='post-update'),
    path('post/<int:pk>/delete/',   PostDeletelView.as_view(),      name='post-delete'),
    path('about/',                  views.about,                    name='blog-about'),
]

user_posts.html:


    {% if is_paginated %}

      {% if page_obj.has_previous %}
        <a class="btn btn-outline-info mb-4" href="?page=1">First</a>
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a>
      {% endif %}

      {% for num in page_obj.paginator.page_range %}
        {% if page_obj.number == num %}
          <a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
          <a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
        {% endif %}
      {% endfor %}

      {% if page_obj.has_next %}
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
      {% endif %}

    {% endif %}
{% endblock content %}

home.html

{% if is_paginated %}

      {% if page_obj.has_previous %}
        <a class="btn btn-outline-info mb-4" href="?page=1">First</a>
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a>
      {% endif %}

      {% for num in page_obj.paginator.page_range %}
        {% if page_obj.number == num %}
          <a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
          <a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
        {% endif %}
      {% endfor %}

      {% if page_obj.has_next %}
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
      {% endif %}

    {% endif %}

post_detail.html

{% extends "blog/base.html" %}
{% block content %}

    <article class="media content-section">
        <img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}" alt="">
        <div class="media-body">
        <div class="article-metadata">
            <a class="mr-2" href="{% url 'user-posts' object.author.username %}">{{ object.author }}</a>
            <small class="text-muted">{{ object.date_posted|date:"M d, Y"}}</small>
            {% if object.author == user %}
                <div>
                    <a class="btn btn-secondary btn-sm mb-1" href="{% url 'post-update' object.id %}">Update</a>
                    <a class="btn btn-danger btn-sm mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
                </div>
            {% endif %}
        </div>
        <h2 class="article-title">{{ object.title }}</h2>
        <p class="article-content">{{ object.content }}</p>
        </div>
    </article>

{% endblock content %}

base.html

{% extends "blog/base.html" %}
{% block content %}

    <article class="media content-section">
        <img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}" alt="">
        <div class="media-body">
        <div class="article-metadata">
            <a class="mr-2" href="{% url 'user-posts' object.author.username %}">{{ object.author }}</a>
            <small class="text-muted">{{ object.date_posted|date:"M d, Y"}}</small>
            {% if object.author == user %}
                <div>
                    <a class="btn btn-secondary btn-sm mb-1" href="{% url 'post-update' object.id %}">Update</a>
                    <a class="btn btn-danger btn-sm mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
                </div>
            {% endif %}
        </div>
        <h2 class="article-title">{{ object.title }}</h2>
        <p class="article-content">{{ object.content }}</p>
        </div>
    </article>

{% endblock content %}
1
  • I guess this is the place {% url 'user-posts' object.author.username %} - check what is the value of object, object.author, object.author.username. This line exists in more than one template.
    – IVNSTN
    Commented Mar 21, 2020 at 15:36

7 Answers 7

3
url(r'^user/(?P<username>\w{0,50})/$', UserPostListView.as_view(), name='user-posts'),

just add it in your url
not this

path('user/<str:username>/', UserPostListView.as_view(),name='user-posts'),
2

I had the same question before. In your user_posts.html and base.html, change all the name of the 'object' stuff to 'post'. Example:

"object.author" -> "post.author", 
"object.title" -> "post.title", 
"object.author.username" -> "post.author.username"

that's a fix for me.

PS: Actually, u didn't post out the above mentioned part of the code. XD

0
1

In the urls.py file -

path('user/<str:username>/', UserPostListView.as_view(),name='user-posts'),

You forget to add backslash after <str:username>

1

i was watching the same course and i had the same problem. following two steps made this work for me: first make sure that you are referring to a correct HTML and then add a forward slash after your url like this:

path('user/<str:username>/', UserPostListView.as_view(), name='user-posts'),

if it didnt work for you use url instead of path like this:

url(r'^user/(?P<username>\w{0,50})/$', UserPostListView.as_view(), name='user-posts'),
1

I ran into the same issue as well.

I found that replacing the following line in blog/urls.py:

urlpatterns = [   

    path('user/<str:username>/', UserPostListView.as_view(), name='user-posts'),
]

with

from django.urls import path, re_path # <- don't forget this import


urlpatterns = [

    re_path(r'^user/(?P<username>\w{0,50})/$', UserPostListView.as_view(), name='user-posts'),
}

solved the issue.

0

I had the same error . My mistake was I misspelled 'object' in this line posts_detail.html

            <a class="mr-2" href="{%url 'user-posts' object.author.username %}">{{ object.author }}</a>

this might not be the reason for your error, but anyone else stuck with this error check for typos in your HTML files.

0

Replace post.author.username with post.author. It helped me.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

] - Stack Overflow">
Skip to main content
Asked
Viewed 10k times
2

I am following django tutorial by @CoreyMSchafer. I got error while practicing i can't find solution to it.

According to my understanding its problem with reversing of url. but can't find out what is wrong

Error: NoReverseMatch at /

Reverse for 'user-posts' with arguments '('',)' not found. 1 pattern(s) tried: ['user/(?P[^/]+)$']

For some reason error is in head of base.html where I'm linking bootstrap.

I also tried removing that link then its giving same error but at line 0 of base.html

views.py:

class UserPostListView(ListView):
    model = Post
    context_object_name = 'posts'
    template_name = 'blog/user_posts.html'

    paginate_by = 5

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Post.objects.all().filter(author= user).order_by('-date_posted')

urls.py file:

from django.urls import path, include
from .views import PostListView, PostDetailView, PostCreateView, PostUpdateView, PostDeletelView, UserPostListView
from . import views

urlpatterns = [
    path('',                        PostListView.as_view(),         name='blog-home'),
    path('user/<str:username>',    UserPostListView.as_view(),     name='user-posts'),
    path('post/<int:pk>/',          PostDetailView.as_view(),       name='post-detail'),
    path('post/new/',               PostCreateView.as_view(),       name='post-create'),
    path('post/<int:pk>/update/',   PostUpdateView.as_view(),       name='post-update'),
    path('post/<int:pk>/delete/',   PostDeletelView.as_view(),      name='post-delete'),
    path('about/',                  views.about,                    name='blog-about'),
]

user_posts.html:


    {% if is_paginated %}

      {% if page_obj.has_previous %}
        <a class="btn btn-outline-info mb-4" href="?page=1">First</a>
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a>
      {% endif %}

      {% for num in page_obj.paginator.page_range %}
        {% if page_obj.number == num %}
          <a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
          <a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
        {% endif %}
      {% endfor %}

      {% if page_obj.has_next %}
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
      {% endif %}

    {% endif %}
{% endblock content %}

home.html

{% if is_paginated %}

      {% if page_obj.has_previous %}
        <a class="btn btn-outline-info mb-4" href="?page=1">First</a>
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a>
      {% endif %}

      {% for num in page_obj.paginator.page_range %}
        {% if page_obj.number == num %}
          <a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
          <a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
        {% endif %}
      {% endfor %}

      {% if page_obj.has_next %}
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
      {% endif %}

    {% endif %}

post_detail.html

{% extends "blog/base.html" %}
{% block content %}

    <article class="media content-section">
        <img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}" alt="">
        <div class="media-body">
        <div class="article-metadata">
            <a class="mr-2" href="{% url 'user-posts' object.author.username %}">{{ object.author }}</a>
            <small class="text-muted">{{ object.date_posted|date:"M d, Y"}}</small>
            {% if object.author == user %}
                <div>
                    <a class="btn btn-secondary btn-sm mb-1" href="{% url 'post-update' object.id %}">Update</a>
                    <a class="btn btn-danger btn-sm mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
                </div>
            {% endif %}
        </div>
        <h2 class="article-title">{{ object.title }}</h2>
        <p class="article-content">{{ object.content }}</p>
        </div>
    </article>

{% endblock content %}

base.html

{% extends "blog/base.html" %}
{% block content %}

    <article class="media content-section">
        <img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}" alt="">
        <div class="media-body">
        <div class="article-metadata">
            <a class="mr-2" href="{% url 'user-posts' object.author.username %}">{{ object.author }}</a>
            <small class="text-muted">{{ object.date_posted|date:"M d, Y"}}</small>
            {% if object.author == user %}
                <div>
                    <a class="btn btn-secondary btn-sm mb-1" href="{% url 'post-update' object.id %}">Update</a>
                    <a class="btn btn-danger btn-sm mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
                </div>
            {% endif %}
        </div>
        <h2 class="article-title">{{ object.title }}</h2>
        <p class="article-content">{{ object.content }}</p>
        </div>
    </article>

{% endblock content %}
1
  • I guess this is the place {% url 'user-posts' object.author.username %} - check what is the value of object, object.author, object.author.username. This line exists in more than one template.
    – IVNSTN
    Commented Mar 21, 2020 at 15:36

7 Answers 7

3
url(r'^user/(?P<username>\w{0,50})/$', UserPostListView.as_view(), name='user-posts'),

just add it in your url
not this

path('user/<str:username>/', UserPostListView.as_view(),name='user-posts'),
2

I had the same question before. In your user_posts.html and base.html, change all the name of the 'object' stuff to 'post'. Example:

"object.author" -> "post.author", 
"object.title" -> "post.title", 
"object.author.username" -> "post.author.username"

that's a fix for me.

PS: Actually, u didn't post out the above mentioned part of the code. XD

0
1

In the urls.py file -

path('user/<str:username>/', UserPostListView.as_view(),name='user-posts'),

You forget to add backslash after <str:username>

1

i was watching the same course and i had the same problem. following two steps made this work for me: first make sure that you are referring to a correct HTML and then add a forward slash after your url like this:

path('user/<str:username>/', UserPostListView.as_view(), name='user-posts'),

if it didnt work for you use url instead of path like this:

url(r'^user/(?P<username>\w{0,50})/$', UserPostListView.as_view(), name='user-posts'),
1

I ran into the same issue as well.

I found that replacing the following line in blog/urls.py:

urlpatterns = [   

    path('user/<str:username>/', UserPostListView.as_view(), name='user-posts'),
]

with

from django.urls import path, re_path # <- don't forget this import


urlpatterns = [

    re_path(r'^user/(?P<username>\w{0,50})/$', UserPostListView.as_view(), name='user-posts'),
}

solved the issue.

0

I had the same error . My mistake was I misspelled 'object' in this line posts_detail.html

            <a class="mr-2" href="{%url 'user-posts' object.author.username %}">{{ object.author }}</a>

this might not be the reason for your error, but anyone else stuck with this error check for typos in your HTML files.

0

Replace post.author.username with post.author. It helped me.

Not the answer you're looking for? Browse other questions tagged or ask your own question.