尝试了Django get_absolute_url NoReverseMatch零模式

时间:2014-11-25 11:49:52

标签: python django

我已经尝试了几天使用get_absolute_url,每当我点击管理页面中的网站视图按钮或点击我的添加表单页面上的提交时,我都会收到此错误:

NoReverseMatch at /admin/r/14/3/ (or NoReverseMatch at /blogs/add/)

Reverse for 'blog_detail' with arguments '()' and keyword arguments '{'pk': 3}' not found. 0 pattern(s) tried: []

如果我去mysite / blog / 3,我确实得到了博客详细信息页面,因为我已经设置好了。所以我知道这个页面有效,但似乎没有尝试找到它,因为它表示尝试了零模式。

我正在查看的文档是:

https://docs.djangoproject.com/en/1.7/topics/class-based-views/generic-editing/ https://docs.djangoproject.com/en/1.7/ref/models/instances/#get-absolute-url

我的代码(我认为至少相关。如果需要更多信息,请告诉我们)

blogs.models.py

from django.contrib.auth.models import User
from django.conf import settings
from django.db import models
from django.core.urlresolvers import reverse
from audit_log.models.fields import CreatingUserField, CreatingSessionKeyField

class Blog(models.Model):
    created_by = CreatingUserField(related_name = "created_categories")
    created_with_session_key = CreatingSessionKeyField()
    created_time = models.DateTimeField(auto_now_add=True)
    blog_title = models.CharField('Blog Title', max_length=200)
    short_description = models.TextField('Short Description', max_length=140)
    blog_image = models.CharField('Image', max_length=200)
    youtube_link = models.URLField('YouTube', max_length=200)
    external_site_link = models.URLField('Website', max_length=200)
    full_blog = models.TextField(max_length=10000)

    def get_absolute_url(self):
        return reverse('blog_detail', kwargs={'pk': self.pk})

    def __unicode__(self):
        return self.blog_name

urls.py

from __future__ import unicode_literals

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.views.generic import TemplateView

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^blogs/', include('blogs.urls', namespace='blogs')),
)

blogs.urls.py

from django.conf.urls import patterns, url
from blogs import views

urlpatterns = patterns('',
    url(
        regex=r'^$',
        view=views.BlogListView.as_view(),
        name='blog_list'
    ),

    url(
        regex=r'^(?P<pk>\d+)/$',
        view=views.BlogDetailView.as_view(),
        name='blog_detail'
    ),

    url(
        regex=r'^add/$',
        view=views.BlogCreate.as_view(),
        name='blog_add'
    ),

    url(
        regex=r'^(?P<pk>\d+)/$',
        view=views.BlogUpdate.as_view(),
        name='blog_update'
    ),

    url(
        regex=r'^(?P<pk>\d+)/delete/$',
        view=views.BlogDelete.as_view(),
        name='blog_delete'
    ),
)

blogs.views.py

from django.shortcuts import render
from django.core.urlresolvers import reverse

from django.views.generic import DetailView
from django.views.generic import RedirectView
from django.views.generic import UpdateView
from django.views.generic import ListView

from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy

# Only authenticated users can access views using this.
from braces.views import LoginRequiredMixin

from .models import Blog

class BlogDetailView(DetailView):
    model = Blog

class BlogListView(ListView):
    model = Blog

class BlogCreate(LoginRequiredMixin, CreateView):
    model = Blog
    fields = ['blog title', 'short_description', 'blog_image',
              'youtube_link', 'external_site_link', 'full_blog']

class BlogUpdate(LoginRequiredMixin, UpdateView):
    model = Blog
    fields = ['blog title', 'short_description', 'blog_image',
              'youtube_link', 'external_site_link', 'full_blog']

class BlogDelete(LoginRequiredMixin, DeleteView):
    model = Blog
    success_url = reverse_lazy('blog_list')

1 个答案:

答案 0 :(得分:3)

您需要使用名称空间blogs来解决网址:

return reverse('blogs:blog_detail', kwargs={'pk': self.pk})