3

This is my first web development project. It is basically an e-voting website. The index page is the login page. So, when the user login using valid credentials, the website will take the user to the voting page where that contains a voting form and some other options like feedback, news updates, etc. in the Navigation bar. The login request is "POST". The vote submission request is also "POST". Here, if the user presses other option like submitting feedback or viewing the candidates, will take the user to that respective pages. And if the user presses the back button, the website should take him to the voting page again where he can again use other facilities voting, viewing candidates, or submit feedback. But the problem I face is if the user presses the back button from the 3rd page i.e., feedback page or viewing candidates page, it shows an error "Confirm Form Resubmission". Help me fix this bug. Thank You in advance.

views.py

from django.shortcuts import render,redirect
from django.contrib.auth.models import User, auth
from django.contrib import messages
from .models import voteoption,receive,received_feedback,updates

# Create your views here.
def index(request):
    return render(request,"index.html")

def login(request):
    
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']

        user = auth.authenticate(username=username,password=password)

        if user is not None:
            auth.login(request, user)
            cand = voteoption.objects.all()
            return render(request,"votepage.html", {'candidates': cand})
        else:
            messages.info(request,'Invalid Email or Password')
            return render(request,'index.html')
    else:

        return render(request,'index.html')

def logout(request):
    auth.logout(request)
    return redirect("/")

def update(request):
    news = updates.objects.all()
    return render(request,'updates.html', {'upd': news})

def contact(request):
    return render(request,'contact.html')

def feedback(request):
    return render(request,'feedback.html')

def feedbacksubmit(request):
    feedback = request.POST['feedback']
    data = received_feedback(response=feedback)
    data.save()
    messages.info(request,'Feedback Submitted Successfully')
    return render(request,'submitted.html')

def candidates(request):
    cand = voteoption.objects.all()
    return render(request,'candidates.html',{'candidates': cand} )

def submitvote(request):
    reg_no = request.POST['reg_no']
    selection = request.POST['selection'] 
    voter = request.POST['voter']

    if receive.objects.filter(mail=voter).exists():
        messages.info(request,'You have already Voted')
        return render(request,'submitted.html')
    else:
        data = receive(reg_no=reg_no,selection=selection,mail=voter)
        data.save()
        messages.info(request,'You have voted Successfully. Thank You!')
        return render(request,'submitted.html')

What changes should I make?

Since this is my first project, there might be a lot of poor coding techniques. So, please suggest me with better codes where I should replace in my code even though if it is not related to this bug.

Naveen A
  • 41
  • 5

1 Answers1

1

You will want to handle successful form submissions with the Post/Redirect/Get pattern to prevent the back button from returning to the previous post. In your Django project this is done by returning a redirect from the view instead of an HttpResponse. Redirect docs

from django.shortcuts import redirect

def view_function(request, ...):
    ... # code processing the POST
    if post_request_successful:
        return redirect(path)
    else:
        return HttpResponse(...)  # or render(...) or whatever you usually return

Where path is a url. Most likely you want to either use the same path in the request request.path or get a path by reversing a url name from your urls.py file (from django.shortcuts import reverse). Reverse docs

Brad Martsberger
  • 1,747
  • 13
  • 7
  • I have added my project files. Can you please help me with the changes I should do? – Naveen A Apr 23 '21 at 03:33
  • Well, it's just like I said in my answer, everywhere in your view functions where you successfully process a POST request, instead of returning `render(...)` you want to `return redirect(request.path)` or `return redirect(reverse(url_name))` and `url_name` is one of the names of your urls you have in urls.py. – Brad Martsberger Apr 23 '21 at 03:37
  • Thank you so much. You have solved my bug. – Naveen A Apr 23 '21 at 04:18