我在/ blog / create /收到此错误NoReverseMatch,任何人都可以帮助我吗?谢谢。
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])
app的urls.py:
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')),
]
当我尝试创建新帖子时,我收到了该错误。我想显示创建的帖子。这是我创建新帖子的表单。
<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>
如果你能解释我的错误,我想。谢谢。
答案 0 :(得分:1)
首先,当您发布有关错误的信息时,请提供完整的错误消息。
在这种情况下,错误是由模型的get_absolute_url
方法引起的。它正在尝试撤销名为&#34; post_detail&#34;传递id和slug参数。但正如您从urls.py中看到的那样,没有这样的网址:那里有一个名为&#34; detail&#34;拿这些参数,或者一个名为&#34; post_detail&#34;没有参数,奇怪地指向你的创建视图。