简体   繁体   中英

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)

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

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

If you want to redirect to the same page after the form successfully submitted return 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM