P3P5 P3P5 - 1 month ago 20
Python Question

NoReverseMatch at /blog/create/

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

views.py

@login_required(login_url='account_login')
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)
new_post.save()
messages.success(request, 'Post creates successfully')
# redirect to new created item detail view
return redirect(new_post.get_absolute_url())
else:
post_form = CreatePostForm()
return render(request,
'blog/post/detail.html',
{'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})


models.py

class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
unique_for_date='publish')
author = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name='blog_posts')
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,
choices=STATUS_CHOICES,
default='draft')
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',
namespace='blogs',
app_name='blog')),
]


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 }}
<fieldset>
<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>
</div>
<div class="input-group" style="margin-bottom: 20px; height: 800px; width: 800px;">
{{ post_form.body }}
</div>
<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>
</div>
</div>
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
</fieldset>
</form>


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

Answer

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.