我正在开发基于Django的RSVP项目。当我想制作一个事件模型并将其与每个用户连接时。它总是有这个错误!活动只有两个字段:user
和name
。
views.py
:
def create_event(request):
if request.method=='POST':
form = CreateEventForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('http://vcm-2971.vm.duke.edu:8080/rsvp/profile')
else:
form= CreateEventForm()
args= {'form':form}
return render(request, 'rsvp/create_event.html',args)
else:
form= CreateEventForm()
args= {'form':form}
return render(request, 'rsvp/create_event.html',args)
models.py
:
class Event(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
name = models.CharField(max_length=100,default='')
forms.py
:
class CreateEventForm(ModelForm):
#name = forms.CharField(max_length=100,required = True)
class Meta:
model = Event
fields=(
'name',
)
答案 0 :(得分:0)
def create_event(request):
if request.method=='POST':
form = CreateEventForm(data=request.POST)
if form.is_valid():
form = form.save(commit=False)
form.user = request.user
form.save()
return redirect('http://vcm-2971.vm.duke.edu:8080/rsvp/profile')
else:
form= CreateEventForm()
args= {'form':form}
return render(request, 'rsvp/create_event.html',args)
else:
form= CreateEventForm()
args= {'form':form}
return render(request, 'rsvp/create_event.html',args)
您需要登录才能使用此表单,或者需要将自定义User instance
传递到Model