djangodjango-viewsdetailsview

Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['blog/article/<int:pk>/']


I created a simple article/blog app. I am having problem in detail view. I created a list view of articles and also created a detail view if someone clicks on one of the acrticle , it takes the to detail view of article .

But when i click on one of the articles , i am getting above error stated in the title. It is also pointing the error location to this code {% url "detail" post.id %} which u can see in 44th or 45th line in Error page i posted in the last.

I tried lots of thing , none worked. I posted all my project files below. you can have look. I would be glad if you guys helps me.

My code goes here :

Views.py

from django.shortcuts import render , redirect , get_object_or_404
from django.shortcuts import render , redirect , get_object_or_404
from .models import Article , members
from django.views.generic import ListView , DetailView
from .forms import create_form

class article_view(ListView):
    model = Article
    template_name = "article.html"
    context_object_name = "articles"

def post_creator(request):
    if request.method == "POST":
        form = create_form(request.POST)
        if form.is_valid():
            form.save()

            return redirect("/blog/home/")

    else:
        form = create_form()
    return render(request , "post_create.html" , {"form":form})

def registration(request):
    if request.method == "POST":
        form = members(request.POST)
        if form.is_valid():
            form.save()

            return redirect("/blog/home/")

    else:
        form = members()
    return render(request , "register.html" , {"form":form})    


class post_detail_view(DetailView):
    model = Article
    template_name = "detail.html"

urls.py

from django.conf.urls import url
from . import views
from django.contrib.auth.views import LoginView , LogoutView
urlpatterns = [

    url("home/" , views.article_view.as_view() , name="blog-home"),
    url("create/" , views.post_creator , name="new_post"),
    url("register/" , views.registration , name="register"),
    url("login/" , LoginView.as_view(template_name="login.html") , name="login"),
    url("logout/" , LogoutView.as_view(template_name="logout.html") , name="logout"),

    url("article/<int:pk>/" , views.post_detail_view.as_view() , name="detail"),

]

article.html (you can assume this as a base.html template)

{% load static %}
<!DOCTYPE html>

<html>

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">



    <title>Home</title>
</head>


<body style="background-image: url('{% static 'wooden.jpg' %}');">
    {% if user.is_authenticated %}
    <fieldset style="position: absolute;top: 20px;right: 0px;width: 100px;">
        <button class="btn btn-outline-info"; onclick="location.href='{% url 'logout' %}'">Logout</button>
    </fieldset>
    <fieldset style="position: absolute;top: 20px;left: 20px;width: 100px;">
        <button class="btn btn-outline-info">{{ user.username }}</button>
    </fieldset>
    <fieldset style="position: absolute;top: 20px;right: 120px;width: 100px;">
        <button class="btn btn-outline-info" onclick="location.href='{% url 'new_post' %}'">Create Post</button>
    </fieldset>
    {% else %}
    <fieldset style="position: absolute;top: 20px;right: 0px;width: 100px;">
        <button class="btn btn-outline-info" onclick="location.href='{% url 'login' %}'">Login</button>
    </fieldset>
    <fieldset style="position: absolute;top: 20px;right: 80px;width: 100px;">
        <button class="btn btn-outline-info" onclick="location.href='{% url 'register' %}'">Signup</button>
    </fieldset>
    {% endif %}

    {% for post in articles %}
        <div>

            <fieldset  style="width:45%;padding: 15px ;background-color: white;margin-left: 150px;margin-bottom: 10px;margin-top: 20px;border: 3px solid #20B2AA;opacity: 0.7; ">
                <h1><a href="{% url 'detail' post.id %}">{{ post.title }}</a></h1>
                <p>Click to see detail Post !</p>
                <small>Published by {{ post.author }} on {{ post.date_pub }}</small></br></br>
            </fieldset>
        </div>

    {% endfor %}

</body>
</html>

detail.html

{% load static %}
<!DOCTYPE html>

<html>

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">



    <title>Home</title>
</head>


<body style="background-image: url('{% static 'wooden.jpg' %}');">
    {% if user.is_authenticated %}
    <fieldset style="position: absolute;top: 20px;right: 0px;width: 100px;">
        <button class="btn btn-outline-info"; onclick="location.href='{% url 'logout' %}'">Logout</button>
    </fieldset>
    <fieldset style="position: absolute;top: 20px;left: 20px;width: 100px;">
        <button class="btn btn-outline-info">{{ user.username }}</button>
    </fieldset>
    <fieldset style="position: absolute;top: 20px;right: 120px;width: 100px;">
        <button class="btn btn-outline-info" onclick="location.href='{% url 'new_post' %}'">Create Post</button>
    </fieldset>
    {% else %}
    <fieldset style="position: absolute;top: 20px;right: 0px;width: 100px;">
        <button class="btn btn-outline-info" onclick="location.href='{% url 'login' %}'">Login</button>
    </fieldset>
    <fieldset style="position: absolute;top: 20px;right: 80px;width: 100px;">
        <button class="btn btn-outline-info" onclick="location.href='{% url 'register' %}'">Signup</button>
    </fieldset>
    {% endif %}


        <div>

            <fieldset style="width:45%;padding: 15px ;background-color: white;margin-left: 150px;margin-bottom: 10px;margin-top: 20px;border: 3px solid #20B2AA;opacity: 0.7; ">
                <h1>{{ object.title }}</h1>


            </fieldset>

            <fieldset style="width:45%;padding: 15px ;background-color: white;margin-left: 150px;margin-bottom: 10px;margin-top: 20px;border: 3px solid #20B2AA;opacity: 0.7; ">
                <p>{{ object.content }}</p>


            </fieldset>

            <fieldset style="width:45%;padding: 15px ;background-color: white;margin-left: 150px;margin-bottom: 10px;margin-top: 20px;border: 3px solid #20B2AA;opacity: 0.7; ">

                <small>Published by {{ object.author }} on {{ object.date_pub }}</small></br></br>
            </fieldset>


        </div>



</body>
</html>

Error page

NoReverseMatch at /blog/home/
Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['blog/article/<int:pk>/']
Request Method:
GET
Request URL:
http://127.0.0.1:8000/blog/home/
Django Version:
2.1
Exception Type:
NoReverseMatch
Exception Value:
Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['blog/article/<int:pk>/']
Exception Location:
C:\Users\nitin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 622
Python Executable:
C:\Users\nitin\AppData\Local\Programs\Python\Python37-32\python.exe
Python Version:
3.7.0
Python Path:
['C:\\Users\\nitin\\Desktop\\my_project',
 'C:\\Users\\nitin\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
 'C:\\Users\\nitin\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
 'C:\\Users\\nitin\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
 'C:\\Users\\nitin\\AppData\\Local\\Programs\\Python\\Python37-32',
 'C:\\Users\\nitin\\AppData\\Roaming\\Python\\Python37\\site-packages',
 'C:\\Users\\nitin\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
Server time:
Mon, 5 Nov 2018 07:32:43 +0000

Error during template rendering
In template C:\Users\nitin\Desktop\my_project\blog\templates\article.html, error at line 44
Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['blog/article/<int:pk>/']
34
    </fieldset>
35
    <fieldset style="position: absolute;top: 20px;right: 80px;width: 100px;">
36
        <button class="btn btn-outline-info" onclick="location.href='{% url 'register' %}'">Signup</button>
37
    </fieldset>
38
    {% endif %}
39

40
    {% for post in articles %}
41
        <div>
42

43
            <fieldset  style="width:45%;padding: 15px ;background-color: white;margin-left: 150px;margin-bottom: 10px;margin-top: 20px;border: 3px solid #20B2AA;opacity: 0.7; ">
44
                <h1><a href="{% url 'detail' post.id %}">{{ post.title }}</a></h1>
45
                <p>Click to see detail Post !</p>
46
                <small>Published by {{ post.author }} on {{ post.date_pub }}</small></br></br>
47
            </fieldset>
48
        </div>
49

50
    {% endfor %}
51

52
</body>
53
</html>

Solution

  • You're confusing the URL syntaxes. url() takes a regex; for the new-style format you need to use path().

    path("article/<int:pk>/" , views.post_detail_view.as_view() , name="detail"),