通过URL传递参数时的Django TypeError

时间:2011-07-17 19:55:28

标签: python django python-2.7 django-1.3

您好我在Django(最新版本)中收到以下错误:

TypeError at /post/1/
post() got an unexpected keyword argument 'post_id'

当我按下主页上的链接以查看帖子的自我时,我会尝试传递帖子的ID(我使用默认的[隐藏]主键,而不是我自己的自定义键)< / p>


这就是我的urls.py对索引和帖子页面的看法:

from django.conf.urls.defaults import patterns, include, url
from journal.models import Post


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

urlpatterns = patterns('journal.views',
    (r'^$', 'index'),
    (r'^post/(?P<id>\d+)/$', 'post'),

这是我的views.py:

from django.http import HttpResponse
from journal.models import Post
from django.template import Context, loader
import os

# Hardcoded Varibles
SITE_ROOT = os.path.join(os.path.dirname(__file__))

# Create your views here.
def index(request):
    latest_post_list = Post.objects.all().order_by('-pub_date')[:10]
    t = loader.get_template(os.path.join(SITE_ROOT, 'templates', 'index.html'))
    c = Context({
    'latest_post_list': latest_post_list,
    })
    return HttpResponse(t.render(c))

def post(request, id):
    return HttpResponse("Hello this is post %" %(post_id))

1 个答案:

答案 0 :(得分:2)

更改

def post(request, id):
    return HttpResponse("Hello this is post %" %(post_id))

def post(request, id):
    return HttpResponse("Hello this is post %s" % id)

我怀疑它会更好一点!