django:CourseNote() 得到了一个意外的关键字参数 'user'
django: CourseNote() got an unexpected keyword argument 'user'
我正在编写一个函数来将表单中的注释保存到数据库,但它一直抛出此错误 CourseNote() got an unexpected keyword argument 'user'
而且我似乎不知道此错误的来源。
view.py
def CourseNote(request, course_slug):
course = Course.objects.get(slug=course_slug)
user = request.user
if request.method == "POST":
course = Course.objects.get(slug=course_slug)
user = request.user
note_title = request.POST.get('note_title')
note_content = request.POST.get('note_content')
# CourseNote.objects.create(user=user, course=course, note_title=note_title, note_content=note_content)
new_note = CourseNote(user=user, course=course, note_title=note_title, note_content=note_content)
new_note.save()
response = 'Saved'
return HttpResponse(response)
urls.py
path('<slug:course_slug>/save-note', views.CourseNote, name="save-note"),
models.py
class CourseNote(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="note_user")
course = models.ForeignKey(Course, on_delete=models.SET_NULL, null=True)
note_title = models.CharField(max_length=200, blank=True, null=True)
note_content = models.TextField(blank=True, null=True)
date = models.DateTimeField(auto_now_add=True)
@sunderam-dubey 将您的视图和模型命名为相同的名称是不好的做法,请更改它
我正在编写一个函数来将表单中的注释保存到数据库,但它一直抛出此错误 CourseNote() got an unexpected keyword argument 'user'
而且我似乎不知道此错误的来源。
view.py
def CourseNote(request, course_slug):
course = Course.objects.get(slug=course_slug)
user = request.user
if request.method == "POST":
course = Course.objects.get(slug=course_slug)
user = request.user
note_title = request.POST.get('note_title')
note_content = request.POST.get('note_content')
# CourseNote.objects.create(user=user, course=course, note_title=note_title, note_content=note_content)
new_note = CourseNote(user=user, course=course, note_title=note_title, note_content=note_content)
new_note.save()
response = 'Saved'
return HttpResponse(response)
urls.py
path('<slug:course_slug>/save-note', views.CourseNote, name="save-note"),
models.py
class CourseNote(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="note_user")
course = models.ForeignKey(Course, on_delete=models.SET_NULL, null=True)
note_title = models.CharField(max_length=200, blank=True, null=True)
note_content = models.TextField(blank=True, null=True)
date = models.DateTimeField(auto_now_add=True)
@sunderam-dubey 将您的视图和模型命名为相同的名称是不好的做法,请更改它