S
S
Some Ipsum2019-04-21 17:41:11
Django
Some Ipsum, 2019-04-21 17:41:11

django error NoReverseMatch: Reverse for 'post_detail_url' with keyword arguments?

Hello everyone, I'm sitting here learning django and I get this error NoReverseMatch: Reverse for 'post_detail_url' with keyword arguments '{u'slug': u'new-slug'}' not found. 1 pattern(s) tried: ['blog/post//']
Here is a python traceback:
File "/home/askar/.local/lib/python2.7/site-packages/django/core/handlers/exception.py" , line 41, in inner
response = get_response(request)
File "/home/askar/.local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/askar/.local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request,
File "/home/askar/Dj/app/blogeng/blog/views.py", line 11, in postlist
return render(request, 'blog/index.html', context={'posts': posts})
File " /home/askar/.local/lib/python2.7/site-packages/django/shortcuts.py", line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/home /askar/.local/lib/python2.7/site-packages/django/template/loader.py", line 68, in render_to_string
return template.render(context, request)
File "/home/askar/.local/lib /python2.7/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/home/askar/.local/lib/python2.7/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/home/askar/.local/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/home/askar/.local/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/askar/. local/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/askar/.local/lib/python2.7/site -packages/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/home/askar/.local/lib/python2.7/site-packages/django/template/base. py", line 199,in_render
return self.nodelist.render(context)
File "/home/askar/.local/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context )
File "/home/askar/.local/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/askar/. local/lib/python2.7/site-packages/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/home/askar/.local/lib/python2.7/site -packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/home/askar/.local/lib/python2.7/site-packages/django/template/ base.py",line 990, in render
bit = node.render_annotated(context)
File "/home/askar/.local/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/askar/.local/lib/python2.7/site-packages/django/template/loader_tags.py", line 72, in render
result = block.nodelist.render(context)
File "/home/askar/ .local/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/askar/.local/lib/python2.7 /site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/askar/.local/lib/python2.7/site-packages/django/template/ defaulttags.py", line 216, in render
nodelist.append(node.render_annotated(context))
File "/home/askar/.local/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render( context)
File "/home/askar/.local/lib/python2.7/site-packages/django/template/base.py", line 1040, in render
output = self.filter_expression.resolve(context)
File "/home /askar/.local/lib/python2.7/site-packages/django/template/base.py", line 708, in resolve
obj = self.var.resolve(context)
File "/home/askar/.local/ lib/python2.7/site-packages/django/template/base.py", line 849, in resolve
value = self._resolve_lookup(context)
File "/home/askar/.local/lib/python2.7/site-packages/django/template/base.py", line 911, in _resolve_lookup
current = current()
File "/home/askar/Dj/app/ blogeng/blog/models.py", line 16, in get_absolute_url
return reverse('post_detail_url', kwargs={'slug': self.slug})
File "/home/askar/.local/lib/python2.7/site -packages/django/urls/base.py", line 91, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/home/askar/.local/lib /python2.7/site-packages/django/urls/resolvers.py", line 497, in _reverse_with_prefix
raise NoReverseMatch(msg)
Here is the code itself:

from __future__ import unicode_literals

from django.shortcuts import render

from .models import Post

# Create your views here.
def postlist(request):
    posts = Post.objects.all()
    return render(request, 'blog/index.html', context={'posts': posts})

def post_detail(request, slug):
    post = Post.objects.get(slug__iexact=slug)
    return render(request, 'blog/post_detail.html', context={'post': post})

{% extends 'blog/base_blog.html' %}

{% block title %}
    post list
{% endblock %}

{% block content %}

    <h1 class="mb-5">Posts:</h1>
    {% for post in posts %}
    <div class="card mb-4">
      <div class="card-header">
        {{ post.date_pub }}
      </div>
      <div class="card-body">
        <h5 class="card-title">{{ post.title }}</h5>
        <p class="card-text">{{ post.body|truncatewords:15 }}</p>
        <a href="{{ post.get_absolute_url }}" class="btn btn-primary">Read</a>
      </div>
    </div>

    {% endfor %}
{% endblock  %}

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.shortcuts import reverse

# Create your models here.

class Post(models.Model):
    title = models.CharField(max_length=150, db_index=True)
    slug = models.SlugField(max_length=150, unique=True)
    body = models.TextField(blank=True, db_index=True)
    date_pub = models.DateTimeField(auto_now_add=True)

    def get_absolute_url(self):
        return reverse('post_detail_url', kwargs={'slug': self.slug})

    def __str__(self):
        return '{}'.format(self.title)

{% extends 'blog/base_blog.html' %}

{% block title %}
    {{ block.title }} - {{ block.super }}
{% endblock %}

{% block content %}
    <h1 class="mt-5">
      {{ post.title }}
    </h1>

    <p>
      {{ post.body }}
    </p>
{% endblock %}

from django.conf.urls import url

from .views import *

urlpatterns = [
url(r'^', postlist, name='posts_list_url'),
url('post/<str:slug>/', post_detail, name='post_detail_url')
]

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question