gpt4 book ai didi

python - 尝试了 Django get_absolute_url NoReverseMatch 零模式

转载 作者:行者123 更新时间:2023-11-30 23:17:51 24 4
gpt4 key购买 nike

我已经尝试使用 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

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

博客.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

url.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')),
)

博客.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'
),
)

博客.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')

最佳答案

您需要使用命名空间 blogs 来寻址 url,如下所示:

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

关于python - 尝试了 Django get_absolute_url NoReverseMatch 零模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27125953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com