Wizard
Wizard

Reputation: 22113

a POST request was judged as NOT POST Method

When I submit a post request from a form, it's processed as not post in the views:

<p>Edit The Topic:</p>
<form  action="{% url "learning_logs:edit_topic" topic.id %}" method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <button name="button">Save Changes</button>
</form>

The views.py, I set test within if request != "POST":

def edit_topic(request, topic_id):
    topic = Topic.objects.get(id=topic_id)

    if request != "POST":
        form = TopicForm(instance=topic)
        # assert request == 'POST'

        print("\tPOST Method in not post condition.\n",
             f"\tRequest Method is {request.__dict__['method']}")

come by with

Quit the server with CONTROL-C.
    POST Method in not post condition.
    Request Method is POST
[16/May/2018 07:14:31] "POST /edit_topic/5 HTTP/1.1" 200 1770

The post method is treated as not post.

What's the problem with my code?

Upvotes: 0

Views: 42

Answers (2)

Evhz
Evhz

Reputation: 9285

Assuming request as request type, try instead:

def edit_topic(request, topic_id):
  topic = Topic.objects.get(id=topic_id)

  if request.method != "POST":
    form = TopicForm(instance=topic)
    # assert request == 'POST'

    print("\tPOST Method in not post condition.\n", f"\tRequest Method is {request.__dict__['method']}")

Upvotes: 2

Henrik Andersson
Henrik Andersson

Reputation: 47212

The correct way of check the request method would be via request.method.

if request.method == 'POST':

What you're currently doing is to check if the request object is the string 'POST', which, it's not since it's the Request object that Django supplies for you.

Upvotes: 2

Related Questions