当前位置:   article > 正文

Django get() got an unexpected keyword argument_django got an unexpected keyword argument

django got an unexpected keyword argument
get() got an unexpected keyword argument

这种错误我现在发现有两种可能造成它的出现:   1. url 和 urls.py中的映射不匹配   2. 在views.py中相应的处理函数缺少参数

 

  1. 在views.py中相应的处理函数缺少参数
  2. urls.py 中的映射关系
  3. urlpatterns = [
  4. path('edit_course/<int:course_id>/',views.EditCourse.as_view(),name='edit_course'),
  5. ]
  6. views.py 中的处理函数
  7. class EditCourse(View):
  8. def get(self,request,course_id): # 这里的 course_id 就是不能少的,要和映射关系对应
  9. course = Course.objects.get(pk=course_id)
  10. teachers = Teacher.objects.all()
  11. categories = CourseCategory.objects.all()
  12. context = {
  13. 'course': course,
  14. 'course_id': course_id,
  15. 'teachers':teachers,
  16. 'categories':categories,
  17. }
  18. return render(request,'cms/edit_course.html',context=context)
  19. def post(self,request,course_id): # 这里的 course_id 就是不能少的,要和映射关系对应
  20. form = EditCourseForm(request.POST)
  21. if form.is_valid():
  22. title = form.cleaned_data.get('title')
  23. category_id = form.cleaned_data.get('category_id')
  24. teacher = form.cleaned_data.get('teacher_id')
  25. video_url = form.cleaned_data.get('video_url')
  26. cover = form.cleaned_data.get('cover_url')
  27. price = form.cleaned_data.get('price')
  28. duration = form.cleaned_data.get('duration')
  29. category = CourseCategory.objects.get(pk=category_id)
  30. pk = form.cleaned_data.get('pk')
  31. print(title,category_id,teacher,video_url,cover,price,duration,pk,end = ' ')
  32. Course.objects.filter(pk=pk).update(title=title,category = category_id,teacher = teacher,video_url = video_url,cover_url=cover,price = price,duration = duration)
  33. return restful.ok()
  34. else:
  35. print("form 不合法")
  36. return restful.params_error(message=form.get_errors())

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/979500
推荐阅读
相关标签
  

闽ICP备14008679号