2016-10-01 166 views
0

我有以下錯誤:的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) 

回答

2

admin網址應該放在第一位:

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'', include('nw.urls', namespace='posts')), 
] 

否則,它會通過在另一個正則表達式匹配您的攔截帖子應用urlpatterns,即:

url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'), 

作爲一個經驗法則,始終保持管理URL在項目的urlpatterns

1

你的情況,這條路線的工作url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),頂部,該功能被稱爲instance = get_object_or_404(Post, slug=slug)

您可以替換這樣的路線

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'', include('nw.urls', namespace='posts')), 
] 

路線的順序很重要