Dingo
Dingo

Reputation: 113

Is there a more elegant way to write this django view?

I've been messing around with django and I have this django view:

def handle_results(request):
    if request.method == "POST" and request.is_ajax():
        # Do something with the post request
    elif request.method == "GET" and request.is_ajax():
        # Do something with the get request
    else:
        # First time in this view, render first element to display
        return render(
            request, "result_page.html", context={"display": arr[0]}
        )

The main idea is, this is supposed to be a Same Page Application, and the first time I'm in this view, I need to render the contents of the array to display to the user, after that, the user can interact with said array via the html (think of it as upvoting or downvoting stuff that's shown). Depending on the user's choice, I get a GET or POST request and need to deal with said request. However, the way I'm implementing this seems not that elegant and I was wondering if there'd be another better way to accomplish what I'm doing.

Thank you so much!

Upvotes: 0

Views: 50

Answers (1)

Starbody
Starbody

Reputation: 155

I would suggest using a class based view

Upvotes: 1

Related Questions