我正在发送一份表格。因此,如果它有效,我将使用消息设置变量message
。因此,如果表单有效,我想重定向到另一个视图,但也传递message
变量。它应该是一个语法问题。
成功提交后,它会重定向到包含网址membership/enroll/studies.views.dashboard
的视图,这当然是错误的。
views.py
def enroll(request):
user = request.user
if request.method == 'POST':
form = SelectCourseYear(request.POST)
if form.is_valid():
student = form.save(commit=False)
student.user = request.user
student.save()
message = 'Successfully Enrolled'
return redirect('studies.views.dashboard', {'message': message,})
else:
form = SelectCourseYear()
return render(request, 'registration/step3.html',)
答案 0 :(得分:2)
考虑使用会话在请求之间存储任意数据:https://docs.djangoproject.com/en/dev/topics/http/sessions/
request.session['message'] = 'Successfully Enrolled'
或者,如果您只想向用户显示消息,您可能会对contrib.messages框架感到满意:https://docs.djangoproject.com/en/dev/ref/contrib/messages/
from django.contrib import messages
messages.success(request, 'Successfully Enrolled')
根据您上面的用例,我猜测contrib.messages更适合您的场景。