我已经排除了 ModelForm 中的一个字段,但它似乎不会影响表单的 display .as_p
。当我什么都没有实例化时,它甚至会显示它!
这是模型表单:
class WorkoutForm(ModelForm):
class Meta:
model = Workout
exclude = ('user',)
和相关的视图:
def addworkout(request, uid):
thisuser = get_object_or_404(User, pk=uid)
if request.method == 'POST':
form = WorkoutForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/u/'+str(uid))
else:
w = Workout(user=thisuser)
#creates a form for thisuser
form = WorkoutForm(instance = w)
return render_to_response('addworkout.html', {'form':form, 'user':thisuser}, RequestContext(request))
还有模板:
<body>
<p>"{{request.path}}" </p>
Add a new workout, {{user.name}}!
<form action="/u/1/addworkout" method="POST">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value= "Submit" />
</form>
</body>