1

Edit: this question is different from django 1.8 NoReverseMatch at error because this one specifically asks how to do it with the ModelViewSet class

I'm trying to make a blog post/facebook wall type app and am getting a NoReverseMatch Error in Django. It happens after trying to submit the post form.

Here's my views.py

from django.shortcuts import render, get_object_or_404, redirect
from wall.models import Post
from .forms import PostForm

def index(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm()

    return render(request, 'wall/index.html', {'form': form})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'wall/post_detail.html', {'post': post})

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^post_detail/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
]

post_detail.html

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

{% block content %}
    <div class="post">
        {% if post.published_date %}
            <div class="date">
                {{ post.published_date }}
            </div>
        {% endif %}
        <p>{{ post.text|linebreaksbr }}</p>
    </div>
{% endblock %}

The error page says

Reverse for 'post_detail' with keyword arguments '{'pk': 5}' not found
1 pattern(s) tried: ['$post_detail/(?P<pk>\\d+)/$']

I've looked at this answer already, but none of the suggestions help. My regex and url names are all spelled correctly.

How do I fix this error?

Here is my base urls.py

from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth import views as auth_views
from . import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', include('wall.urls'), name='index'),
    url(r'^accounts/register/$', views.register, name='register'),
    url(r'^accounts/register/complete/$', views.registration_complete, name='registration_complete'),
    url(r'^accounts/login/$', auth_views.login, name='login'),
    url(r'^accounts/logout/$', auth_views.logout, name='logout'),
    url(r'^accounts/loggedin/$', views.logged_in, name='loggedin'),
]
Matt
  • 2,232
  • 8
  • 35
  • 64

2 Answers2

2

I think your issue is on the redirect after the form submission, change

return redirect('post_detail', pk=post.pk)

to

return redirect(reverse('post_detail', kwargs={'pk': post.pk}))

(to import reverse use : from django.core.urlresolvers import reverse)


As Alasdair pointed out, a $ on include was also missing from your base urls.py

Horai Nuri
  • 5,358
  • 16
  • 75
  • 127
  • No, I still get the same error. – Matt Jun 14 '17 at 19:30
  • Do you have the ability to enter the created page by the url or is it still a problem on form submit ? – Horai Nuri Jun 14 '17 at 19:32
  • It says page not found. I tried both http://127.0.01:8000/post_detail and http://127.0.01:8000/post_detail/1 (a made up primary key) – Matt Jun 14 '17 at 19:33
  • These type of errors can be for multiple reasons, try adding `/` at the end of your url just like that : `127.0.01:8000/post_detail/1/`, does it work ? (the url you're trying to access has to exist otherwise it'll return a 404) – Horai Nuri Jun 14 '17 at 19:36
  • It gives me the same error – Matt Jun 14 '17 at 19:37
  • What happen if you replace `post = get_object_or_404(Post, pk=pk)` by `post = Post.objects.filter(pk=pk) ` ? Could you also include your base urls.py file ? – Horai Nuri Jun 14 '17 at 19:45
  • That didn't work either, same error. – Matt Jun 14 '17 at 19:47
  • Have you restarted your server ? – Horai Nuri Jun 14 '17 at 19:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146696/discussion-between-matthew-drill-and-lindow). – Matt Jun 14 '17 at 19:49
  • You don't need to use `reverse` with `redirect`. The advantage of using the `redirect` shortcut here is that it's simpler than `HttpResponseRedirect(reverse(...))`. – Alasdair Jun 14 '17 at 21:11
1

It looks as if you have a dollar in your regex when you include your app's urls. Remove it.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • That didn't work. – Matt Jun 14 '17 at 19:44
  • What didn't work? If you have removed the dollar from the include then that should fix the error. Make sure you have restarted your server. – Alasdair Jun 14 '17 at 19:46
  • I removed the $ from the url pattern and I restarted the server. What do you mean by removing it from the include? – Matt Jun 14 '17 at 19:48
  • 1
    @MatthewDrill He meant to remove the $ from include on your base urls.py, can you show the file to us ? – Horai Nuri Jun 14 '17 at 19:49
  • It sounds like you remove the dollar from the wrong place. See the [duplicate question](https://stackoverflow.com/questions/30720413/django-1-8-noreversematch-at-error) for an example. – Alasdair Jun 14 '17 at 19:54