gpt4 book ai didi

python - Django TypeError get_object_or_404() 至少需要 1 个参数(给定 0 个)

转载 作者:行者123 更新时间:2023-12-01 04:59:59 25 4
gpt4 key购买 nike

我正在创建一个博客并学习 django。我正在尝试合并包含 sluglines 和帖子 id 的网址。

当我单击管理面板中的“现场查看”链接时,出现以下错误:NoReverseMatch at/admin/r/7/2/未找到带有参数 '(u'test-post-3', '2')' 和关键字参数 '{}' 的 'article' 反向匹配。尝试了 0 个模式:[]

当我手动输入网址时,出现以下错误:/articles/test-post-3,2/get_object_or_404() 处的 TypeError 至少需要 1 个参数(给定 0 个)这是我的代码:

views.py:

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader
from django.core.urlresolvers import reverse
from django.views import generic
from django.utils import timezone

# Create your views here.

from articles.models import Content

class IndexView(generic.ListView):
template_name = 'articles/index.html'
context_object_name = 'latest_articles_list'

def get_queryset(self):
return Content.objects.filter(
published_date__lte=timezone.now()
).order_by('-published_date')[:5]

def detail(request, slugline, id):
article = get_object_or_404(pk=id)
return render(request, 'articles/detail.html', {'article': article})

url.py:

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

urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name = 'index'),
#url(r'^(?P<slugline>[-\w\d]+), (?P<pk>\d+)/$', views.DetailView.as_view(), name='detail')
url(r'^(?P<slugline>[-\w\d]+),(?P<id>\d+)/$', view=views.detail, name='article'),
)

模型.py:

from django.db import models
from django.db.models import permalink
from django.utils import timezone
import datetime
# Create your models here.


class Content(models.Model):
title = models.CharField(max_length=100, unique=True)
slugline = models.SlugField(max_length=100, unique=True)
body = models.TextField()
published_date = models.DateTimeField('date published')

def __unicode__(self):
return self.title

@permalink
def get_absolute_url(self):
# return ('article', (), {
# 'slugline': self.slugline,
# 'id': self.id,
# })
from django.core.urlresolvers import reverse
return reverse('article', args=[self.slugline, str(self.id)])

def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.published_date <= now
was_published_recently.admin_order_field = 'published_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'

主url.py:

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'the_Bluntist.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),

url(r'^articles/', include('articles.urls', namespace="articles")),
url(r'^admin/', include(admin.site.urls)),
)

最佳答案

您对 get_object_or_404 的使用不当:

Docstring:

get_object_or_404(klass, *args, **kwargs)

Uses get() to return an object, or raises a Http404 exception if the object does not exist.

klass may be a Model, Manager, or QuerySet object. All other passed arguments and keyword arguments are used in the get() query.

Note: Like with get(), an MultipleObjectsReturned will be raised if more than one object is found.

您可以按如下方式进行:

  • article = get_object_or_404(Article, pk=id)
  • article = get_object_or_404(Article.objects, pk=id))
  • article = get_object_or_404(Article.objects.all(), pk=id))
  • article = get_object_or_404(Article.objects.filter(pk=id))

关于python - Django TypeError get_object_or_404() 至少需要 1 个参数(给定 0 个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26476206/

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