- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个博客并学习 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)
Usesget()
to return an object, or raises aHttp404
exception if the object does not exist.
klass
may be aModel
,Manager
, orQuerySet
object. All other passed arguments and keyword arguments are used in theget()
query.
Note: Like withget()
, anMultipleObjectsReturned
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/
我需要一个带有条件的正则表达式: 最少 6 个字符,最多 50 个字符 必须包含 1 个字母 必须包含 1 个数字 可能包含特殊字符,例如!@#$%^&*()_+ 目前我有模式:(?!^[0-9]*$
我想检查密码是否至少包含一个字母和一个数字。接受特殊字符但不是必需的... 那将是一个简单的密码检查器。 最佳答案 您可以使用先行断言来检查任何数字和任何字母的存在,如下所示: ^(?=.*[a-zA
我需要找到生产至少三种不同型号电脑的制造商。结果集:制造商、型号数量。 我的 table 是产品(制造商、型号、类型)PC(代码、型号、速度、内存、高清、CD、价格) 我现在已经这样做了 SELECT
在我正在处理的哈希字典中,根据 GDB,数组的大小已从 500 调整到 1000。它在尝试添加时崩溃的数组索引是 799,所以它没有超出范围......我不是确定它为什么会出现段错误,尤其是在这样一条
我需要一个正则表达式来检查字符串的长度是否至少为 10 个字符。这些字符是什么并不重要。 谢谢 最佳答案 您可以使用: .{10,} 自 .默认情况下不匹配换行符,您必须使用合适的修饰符(如果您的正则
我有以下代码: val num1: Int? = someObject.number val num2: Int? = anotherObject?.anotherNumber val numToFa
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我一直在进行查询以获取用户列表及其各自的组。我知道了。 但是有一些限制: 如果用户只是 group5 的成员,则不应出现在列表中 如果是 group5 和其他组的成员,它应该出现在列表中 我可以获得不
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我需要忽略任何一个空格,并且应该匹配至少大于一个空格的空格... "MARY HAD A LITTLE LAMB" 我希望 "MARY", "HAD A LITTLE", "
如何在仅数字验证中允许空格(至少 8 位数字)?当允许空格时,输入电话号码会更容易。例如0400 123 456、9699 1234。 到目前为止,这是我的代码,我只进行了最少 8 位数字验证: jQ
我想将以下内容重写为 HTML 模式: if (/\S/.test(myString)) { // string is not empty and not just whitespace }
我的想法是使用 php 或 js 创建大量的 div。 (我从昨天开始写js。) 所以我自己给定的任务是使用 php 或 js 或两者生成一个 div 网格。到目前为止的想法是有一个通用的 div 作
显示字符串中至少有 10 个数字字符的正则表达式。 可以多于 10 个,但不能少于 10 个。在随机位置可以有任意数量的其他字符,将数字分隔开。 示例数据: (123) 456-7890 123-45
我目前在 Firefox 中使用边框动画时遇到问题,从无到 50px。 Chrome 非常好,但 Firefox 不是,我想同样的问题也会出现在 Opera 中。 这里有一个 fiddle 来向您展示
作为 ruby 挑战训练的一部分,我被要求创建一个方法 substrings(string),它将接受一个字符串,并返回一个包含所有子字符串的数组。 即 substrings("cat") #=>
我目前有这段代码,它检查数组中的所有元素是否相同。如果是这种情况,则返回 true def all_equal(lst): """ >>> all_equal([1,1,1,1,1,1,1])
当我注意到 Libre Office 电子表格显示远低于 2^53 的数字的错误值时,我正在做一些计算以计划改进我的主要筛选器的实现,这是 FoxPro 和许多其他精确整数计算的限制内部使用 C do
我有一个复选框,我想检查用户是否填写了它后面的文本框。必须至少包含 5 个字符才能选中复选框。 pro)) echo 'checked' ?>> pro?>">Please supply your I
我正在为我的食谱设计一个数据库。我在我的设计中创建了多个表格:书籍、作者、食谱、成分,对于所有这些项目,我想将媒体(图像或视频)链接到所有这些表格中的项目。 我在想这样的设计: media_id, r
我是一名优秀的程序员,十分优秀!