Reverse for 'user-post' not found. 'user-post' is not a valid view function or pattern name

i have created a blog app. when i try to create new posts is shows the following error. i have double check everything. Now i really don’t know what to do.

here are project ```
path(‘register/’, user_views.register,name=‘register’),
path(‘profile/’, user_views.profile,name=‘profile’),
path(‘login/’, auth_views.LoginView.as_view(template_name=‘users/login.html’),name=‘login’),
path(’’, include(‘Bblog.urls’)),

here are app**

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

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/', PostDeleteView.as_view(), name='post-delete'),
    path('about/', views.about,name='blog-about'),
    path('search/',, name='blog-search'),

here are app

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from .models import Post
from django.core.paginator import Paginator
from django.contrib.auth.models import User
from django.db.models import Q
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView

def homepage(request):
    context = {
        'posts': Post.objects.all()
    return render(request, 'Bblog/home.html', context)

class PostListView(ListView):
    model = Post
    template_name = 'Bblog/home.html'
    context_object_name = 'posts'
    ordering = ['-date_posted']
    paginate_by = 3

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

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

class PostDetailView(DetailView):
    model = Post

class PostCreateView(LoginRequiredMixin, CreateView):
    model = Post
    fields = ['title', 'content']

    def form_valid(self, form): = self.request.user
        return super().form_valid(form)

class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Post
    fields = ['title', 'content']

    def form_valid(self, form): = self.request.user
        return super().form_valid(form)

    def test_func(self):
        post = self.get_object()
        if self.request.user ==
            return True
        return False

class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Post
    success_url = '/'

    def test_func(self):
        post = self.get_object()
        if self.request.user ==
            return True
        return False

def about(request):
    return render(request, 'Bblog/about.html', )

def search(request):
    query = request.GET['query']
    posts = Post.objects.filter(Q(title__icontains=query) | Q(content__icontains=query)
    context = {
        'posts': posts
    return render(request, 'bblog/search.html', context)

You didn’t include your template for the page generating this error, so this is a bit of a guess - your error message says reverse for user-post not found. Your name assigned above is for user-posts, I don’t see a definition for user-post.



sir here are my all templates…being a new user i cant not upload. could you plz take a look.

The error is at this line

You’re referencing a url name user-post, but the name defined is user-posts.

1 Like

sir i don’t know how to thank you. i am glad that a veteran like you helped me.Thank you very much.

  • i have same issue Reverse for ‘mobile’ not found. ‘mobile’ is not a valid view function or pattern name.

  • mobile.html…

  • {% extends ‘app/base.html’ %}

  • {% load static %}

  • {% block title %}Mobile{% endblock title %}

  • {% block main-content %}

  • All Mobile

  • Redmi

  • <a href="{% url ‘Mobiledata’ ‘Samsung’ %}" class=“list-group-item list-group-item-action” aria-current">Samsung

  • {for product in mobiles%}

  • <a href="{% url ‘product-detail’ %}"

  • class= “btn”>

  • {{prouct.title}}
  • Rs. {{product.discounted_price}}
  • <small class=“fw-light” text-decoration-line-through">{{product.selling-price}}

  • {% endfor %}

  • {% endblock main-content %}


  • from django.urls import path

  • from app import views

  • from django.conf import settings

  • from django.conf.urls.static import static

  • urlpatterns = [

  • path('',views.ProductView.as_view(),name="home"),
  • path('product-detail/<int:pk>', views.ProductDetailView.as_view(), name='product-detail'),
  • path('cart/', views.add_to_cart, name='add-to-cart'),
  • path('buy/', views.buy_now, name='buy-now'),
  • path('profile/', views.profile, name='profile'),
  • path('address/', views.address, name='address'),
  • path('orders/', views.orders, name='orders'),
  • path('changepassword/', views.change_password, name='changepassword'),
  • path('mobile/<slug:data>',, name='Mobiledata'),
  • path('login/', views.login, name='login'),
  • path('registration/', views.customerregistration, name='customerregistration'),
  • path('checkout/', views.checkout, name='checkout'),
  • ] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)


  • from django.shortcuts import render

  • from django.views import View

  • from .models import Customer,Product,Cart,OrderPlaced

  • def home(request):

  • return render(request, ‘app/home.html’)

  • class ProductView(View):

  • def get(self,request):
  •     topwears=Product.objects.filter(category='TW')
  •     bottomwears=Product.objects.filter(category='BW')
  •     mobiles=Product.objects.filter(category='M')
  •     return render(request,'app/home.html/',
  •     {'topwear':topwears,'bottomwears':bottomwears,'mobiles':mobiles})
  • def product_detail(request):

  • return render(request, ‘app/productdetail.html’)

  • class ProductDetailView(View):

  • def get(self,request,pk):
  •     product=Product.objects.get(pk=pk)
  •     return render(request,'app/productdetail.html',{'product':product})
  • def add_to_cart(request):

  • return render(request, ‘app/addtocart.html’)

  • def buy_now(request):

  • return render(request, ‘app/buynow.html’)

  • def profile(request):

  • return render(request, ‘app/profile.html’)

  • def address(request):

  • return render(request, ‘app/address.html’)

  • def orders(request):

  • return render(request, ‘app/orders.html’)

  • def change_password(request):

  • return render(request, ‘app/changepassword.html’)

  • def mobile(request, data=None):

  • if data == None:

  •     mobiles=Product.objects.filter(category='M')
  • elif data=='Redmi’or data==‘Samsung’:

  •     mobiles=Product.objects.filter(category='M').filter(brand=data)
  • elif data==‘below’:

  •     mobiles=Product.objects.filter(category='M').filter(discounted_price__lt=10000)
  • elif data==‘above’:

  •     mobiles=Product.objects.filter(category='M').filter(discounted_price__gt=10000)
  • return render(request, ‘app/mobile.html’,{‘mobiles’:mobiles})

  • def login(request):

  • return render(request, ‘app/login.html’)

  • def customerregistration(request):

  • return render(request, ‘app/customerregistration.html’)

  • def checkout(request):

  • return render(request, ‘app/checkout.html’)

This appears to be a different problem on a different project, it’s ok to open up a new thread of discussion to address the issue you’re encountering.

Also, when posting code here, please enclose it between lines consisting of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This allows the forum software to keep your code properly formatted, which is very important with Python.

1 Like