Django: TypeError topic() got an unexpected keyword argument 'topics_id'

urls.py

#coding=utf-8
'''定义learning_logs的URL模式'''

from django.conf.urls import url
from . import views

urlpatterns = [
	#主页
    url(r'^$',views.index,name='index'),

    #显示所有的主题
    url(r'^topics/$',views.topics,name='topics'),

    #特定主题的详细页面
    url(r'^topics/(?P<topics_id>\d+)/$',views.topic,name='topic'),
]



views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

from .models import Topic

# Create your views here.

def index(request):
	'''学习笔记的主页'''
	return render(request,'learning_logs/index.html')

def topics(request):
	'''显示所有的主题'''
	topics=Topic.objects.order_by('date_added')
	context={'topics':topics}
	return render(request,'learning_logs/topics.html',context)

def topic(request,topic_id):
	'''显示单个主题及其所有条目'''
	topic=Topic.objects.get(id=topic_id)
	entries=topic.entry_set.order_by('-date_added')
	context={'topic':topic,'entries':entries}
	return render(request,'learning_logs/topic.html',context)



models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Topic(models.Model):
	'''用户学习的主题'''
	text=models.CharField(max_length=200)
	date_added=models.DateTimeField(auto_now_add=True)

	def __unicode__(self):
		'''返回模型的字符串表示'''
		return self.text

class Entry(models.Model):
	'''学到的有关某个主题的具体知识'''
	topic=models.ForeignKey(Topic)
	text=models.TextField()
	date_added=models.DateTimeField(auto_now_add=True)

	class Meta:
		verbose_name_plural='entries'

	def __unicode__(self):
		'''返回模型的字符串表示'''
		return self.text[:50]+"..."

urls.py中将?P<topic_id>匹配到的值存储到topics_id中

views.py接受正则表达式(?P<topic_id>\d+)捕获的值,使用get来获取到指定主题

urls.py与views.py中的参数需保持一致,上面代码中urls.py中的参数为topics_id,views.py中的参数为topic_id,所有报错


参考:https://stackoverflow.com/questions/37254829/django-got-an-unexpected-keyword-argument-id


  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值