我有以下錯誤:的Django /管理/找不到網頁(404)
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Raised by: nw.views.post_detail
但我可以例如在進入到http://127.0.0.1:8000/admin/nw/
如果我刪除代碼的所有post_detail功能/配件那麼我可以輸入到http://127.0.0.1:8000/admin/,它的工作原理,所以這個post_detail是錯誤的。
nw.views.post_detail:
def post_detail(request, slug=None):
instance = get_object_or_404(Post, slug=slug)
context = {
"instance": instance,
}
return render(request, "post_detail.html", context)
urls.py:
urlpatterns = [
url(r'', include('nw.urls', namespace='posts')),
url(r'^admin/', admin.site.urls),
]
和
urlpatterns = [
url(r'^$', post_list, name='list'),
url(r'^create/$', post_create),
url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
url(r'^(?P<slug>[\w-]+)/delete/$', post_delete),
]
post_detail.html:
{% extends 'base.html' %}
{% block head_title %}{{ instance.title }} | {{ block.super }}{% endblock head_title %}
{% block content %}
<div class="instance">
<h1>{{ instance.title }}</h1>
<div class="date">{{ instance.published }}</div>
<p>{{ instance.text|linebreaksbr }}</p>
<a href="https://www.facebook.com/sharer/sharer.php?u={{ request.build_absolute_uri }}">
Facebook
</a>
<a href="https://twitter.com/home?status={{ share_string }}%20{{ request.build_absolute_uri }}">
Twitter
</a>
<a href='https://plus.google.com/share?url={{ request.build_absolute_uri }}'>google</a>
<a href="https://www.linkedin.com/shareArticle?mini=true&url={{ request.build_absolute_uri }}&title={{ instance.title }}&summary={{ share_string }}&source={{ request.build_absolute_uri }}">
Linkedin
</a>
<a href="http://www.reddit.com/submit?url={{ request.build_absolute_uri }}&title={{ share_string }}.">Reddit</a>
</div>
{% endblock %}
models.py:
class Post(models.Model):
author = models.ForeignKey('auth.User', default=1)
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
text = models.TextField()
published = models.DateTimeField(auto_now=False, auto_now_add=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("posts:detail", kwargs={"slug": self.slug})
class Meta:
ordering = ["-published"]
def create_slug(instance, new_slug=None):
slug = slugify(instance.title)
if new_slug is not None:
slug = new_slug
qs = Post.objects.filter(slug=slug).order_by("-id")
exists = qs.exists()
if exists:
new_slug = "%s-%s" %(slug, qs.first().id)
return create_slug(instance, new_slug=new_slug)
return slug
def pre_save_post_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = create_slug(instance)
pre_save.connect(pre_save_post_receiver, sender=Post)