pythondjangodjango-viewshttpresponsereversi

Is there a way to render a request within an if Statement in django?


I am trying to write a django web application for Reversi Game. I have a problem with rendering the new table to the website.

views.py

def table(request):
    if request.method == "POST":
        coord = request.POST.keys()
        crd = list(coord)[1]
        x = crd.split("_")
        r = int(x[0]) - 1
        c = int(x[1]) - 1
        reversi = ReversiGame()
        grid = draw_grid(reversi)
        ctxt = {"table": grid}
        return render(request, 'table.html', context=ctxt)

template

{% extends 'base.html' %}
{% block main_content %}
    <div style="text-align: center; width: auto;
    height:auto; margin-right:auto; margin-left:200px;">
    {% for r in table %}
        <div style="float:left">
        {% for c in r %}
             <form action="" method="post">
            {% csrf_token %}
            {% if c == 2 %}
                <input type="submit" style="background: #000000; width:50px; height:50px;
                            color:white;"
                       name="{{forloop.counter}}_{{forloop.parentloop.counter}}" value="2">
            {% elif c == 1 %}
                <input type="submit" style="background: #ffffff;  width:50px; height:50px;"
                       name="{{forloop.counter}}_{{forloop.parentloop.counter}}" value="1">
            {% else %}
                <input type='submit' style="background: #c1c1c1;  width:50px; height:50px;"
                       name="{{forloop.counter}}_{{forloop.parentloop.counter}}" value="o">
            {% endif %}
             </form>
        {% endfor %}
        </div>
    {% endfor %}
    </div>
{% endblock %}

urls.py

urlpatterns = [
    path('', views.HomeView.as_view(), name="home"),
    path('signup/', views.SignUpView.as_view(), name='signup'),
    path('profile/<int:pk>/', views.ProfileView.as_view(), name='profile'),
    path('table/', views.table, name='table')
]

When I try to return a HttpResponse within the request.method, the following error is being raised: The view GameConfiguration.views.table didn't return an HttpResponse object. It returned None instead.

If I move a tab to left the return render(request, 'table.html', context=ctxt), then ctxt variable, which is the new board, is not being recognised (it says that it is used before assignment), which means that I do not have access to the newly drawn table.

I need the row and the col from the POST method in order to flip the board and switch the player.

I sincerely appreciate your time! Thank you!


Solution

  • Your view function only returns a response when request.method == "POST". When you visit the page in a browser and receive the error The view ... didn't return an HttpResponse object., that's because the request made through the browser has request.method == "GET".

    You can fix your view method by adding a return method outside of the if statement:

    def table(request):
        if request.method == "POST":
            # Here is where you capture the POST parameters submitted by the
            # user as part of the request.POST[...] dictionary.
            ...
            return render(request, 'table.html', context=ctxt)
    
        # When the request.method != "POST", a response still must be returned.
        # I'm guessing that this should return an empty board.
        reversi = ReversiGame()
        grid = draw_grid(reversi)
        return render(request, 'table.html', context={"table": grid})