get() got an unexpected keyword argument
这种错误我现在发现有两种可能造成它的出现: 1. url 和 urls.py中的映射不匹配 2. 在views.py中相应的处理函数缺少参数
在views.py中相应的处理函数缺少参数
urls.py 中的映射关系
urlpatterns = [
path('edit_course/<int:course_id>/',views.EditCourse.as_view(),name='edit_course'),
]
views.py 中的处理函数
class EditCourse(View):
def get(self,request,course_id): # 这里的 course_id 就是不能少的,要和映射关系对应
course = Course.objects.get(pk=course_id)
teachers = Teacher.objects.all()
categories = CourseCategory.objects.all()
context = {
'course': course,
'course_id': course_id,
'teachers':teachers,
'categories':categories,
}
return render(request,'cms/edit_course.html',context=context)
def post(self,request,course_id): # 这里的 course_id 就是不能少的,要和映射关系对应
form = EditCourseForm(request.POST)
if form.is_valid():
title = form.cleaned_data.get('title')
category_id = form.cleaned_data.get('category_id')
teacher = form.cleaned_data.get('teacher_id')
video_url = form.cleaned_data.get('video_url')
cover = form.cleaned_data.get('cover_url')
price = form.cleaned_data.get('price')
duration = form.cleaned_data.get('duration')
category = CourseCategory.objects.get(pk=category_id)
pk = form.cleaned_data.get('pk')
print(title,category_id,teacher,video_url,cover,price,duration,pk,end = ' ')
Course.objects.filter(pk=pk).update(title=title,category = category_id,teacher = teacher,video_url = video_url,cover_url=cover,price = price,duration = duration)
return restful.ok()
else:
print("form 不合法")
return restful.params_error(message=form.get_errors())