[英]Django How do i Insert data without refreshing or reloading page
How to do in django to return with the current page after the user insert their data (request.post)在用户插入数据后如何在 Django 中返回当前页面(request.post)
scenario:设想:
If the teacher insert the grades of their students, the current page will reload or refresh .如果教师插入学生的成绩,当前页面将重新加载或刷新。
I don't know if I can achieve it by using redirect.我不知道我是否可以通过使用重定向来实现它。
def grades(request):
V_insert_data = StudentSubjectGrade(
...
)
V_insert_data.save()
//redirect the same page
def teacher(request):
if request.method != 'POST':
raise Http404('Only POSTs are allowed')
try:
......
return render(request, 'Homepage/index.html')
except EmployeeUser.DoesNotExist:
messages.warning(request, 'Your Username and password are Incorrect.')
….
return render(request, 'Homepage/TeacherLogin.html')
urls.py网址.py
path('teacher/', Homepage.views.teacher, name='teacher'),
path('grades/', Homepage.views.grades, name='grades'),
this is the error i get这是我得到的错误
UPDATE更新
when I change my当我改变我的
response = redirect(teacher)
return response
to this对此
return redirect(reverse('teacher'))
i get the same result我得到相同的结果
UPDATE again再次更新
when I tried this当我尝试这个时
def grades(request):
V_insert_data = StudentSubjectGrade(
...
)
V_insert_data.save()
return HttpResponseRedirect(request.path_info)
I get this error我收到这个错误
ive already clear my cookies but still this error appear我已经清除了我的 cookie,但仍然出现此错误
If you want to redirect to the same page after the form successfully submitted return HttpResponseRedirect
.如果您想在表单成功提交后重定向到同一页面,请返回
HttpResponseRedirect
。 Let me give a sample view here让我在这里给出一个示例视图
from django.http import HttpResponseRedirect
def grades(request):
if request.method == 'POST':
# do your stuff here (saving data to db)
return HttpResponseRedirect(request.path_info)
return render(request, template, {'form': your_form})
def grades(request):
V_insert_data = StudentSubjectGrade(
...
)
V_insert_data.save()
return redirect(reverse('teacher'))
def teacher(request):
if request.method == 'POST':
try:
......
return render(request, 'Homepage/index.html')
except EmployeeUser.DoesNotExist:
messages.warning(request, 'Your Username and password are Incorrect.')
….
return render(request, 'Homepage/TeacherLogin.html')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.