P3P5 P3P5 -4 years ago 214
Python Question

NoReverseMatch at /blog/create/

I am getting this error NoReverseMatch at /blog/create/, can anyone help me? Thank you.


def create_post(request):
if request.method == 'POST':
post_form = CreatePostForm(request.POST)
if post_form.is_valid():
new_post = post_form.save(commit=False)
new_post.author = request.user
new_post.slug = slugify(new_post.title)
messages.success(request, 'Post creates successfully')
# redirect to new created item detail view
return redirect(new_post.get_absolute_url())
post_form = CreatePostForm()
return render(request,
{'post_form': post_form})

def post_detail(request, id, slug):
post = get_object_or_404(Post, id=id, slug=slug)
return render(request, 'blog/post/detail.html', {'section': 'blogs',
'post': post})


class Post(models.Model):
('draft', 'Draft'),
('published', 'Published'),
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
author = models.ForeignKey(settings.AUTH_USER_MODEL,
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
objects = models.Manager() # The default manager
published = PublishedManager() # Our custom manager
tags = TaggableManager()

class Meta:
ordering = ('-publish',)

def __str__(self):
return self.title

def get_absolute_url(self):
return reverse('blogs:post_detail',
args=[self.id, self.slug])

urls.py for the app:

urlpatterns = [
# post views
url(r'^$', views.post_list, name='post_list'),
url(r'^create/$', views.create_post, name='post_detail'),
url(r'^detail/(?P<id>\d+)/(?P<slug>[-\w]+)/$', views.post_detail, name='detail'),

main urls.py

urlpatterns = [

url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('blog.urls',

when i try to create a new post i get that error. I want to display the post thats created. Here is my form for creating the new post.

<form class="form-horizontal" method="post" action=".">
{% csrf_token %}
{{ post_form.media }}
<div class="form-inline">
<div class="form-group" style="margin-bottom: 20px;">
{% render_field post_form.title class="form-control" placeholder="Blog title" %}
{% render_field post_form.status class="form-control" %}
<div class="input-group" style="margin-bottom: 20px; height: 800px; width: 800px;">
{{ post_form.body }}
<div class="control-group">
<!-- Button -->
<div class="controls" style="margin-bottom: 20px">
<button class="btn btn-lg btn-success" style="min-width: 300px">Save</button>
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}

And I would like if you could explain me what was wrong. Thank you.

Answer Source

Firstly, when you post about an error, please give the full error message.

In this case the error is caused by the get_absolute_url method of your model. It's trying to reverse a url called "post_detail" passing id and slug arguments. But as you can see from the urls.py, there isn't such a url: there's one called "detail" that takes those arguments, or one called "post_detail" that takes no arguments and strangely points to your create view.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download