POST or METHOD not being referenced in view

I’ll admit that for this I was using a tutorial on how to create a login page, however, I came across a weird and unexpected error, for some reason method and POST were not referenced from any definition, and remained simply colorless in the code. I’m sorry if I’m not being super helpful with the description I’m just new to the process, but here’s my view code so far if you are curious

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages

def login_account(request):
      if request.method == "POST":
          username = request.POST["username"]
          password = request.POST["password"]
          user = authenticate(request, username=username, password=password)
          if user is not None:
              login(request, user)
              # Redirect to a success page.
              return redirect('home')
          else:
              # Return an 'invalid login' error message.
              messages.success(request, ("Error Logging In"))
              return redirect('registration/login.html')
      else:
          return render(request, "registration/login.html", {})

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

1 Like

Can you be more specific about what you mean by this:

Are you referring simply to how it’s appearing in your editor? If so, that’s not a Django issue and I am not sure what information you’re looking for here.

Or are you getting some kind of error when you’re running this? If so, please post the complete error and traceback.

It’s both
In terms of how it appears in the editor, besides just being off-color, when I try to tie it to a specific definition/origin, it doesn’t lead to any source of code.
But also on the site itself, I’m trying to use this to log in. However from what I am able to gather anytime I try to enter in the password or username instead of either giving me an error message or redirecting me, nothing happens.

We’ll need to see the login.html template.

You may want to add a couple of print statements in your view to let you see what data you are seeing at different points here. For example, you might want to add a print(request.POST) before the if request.method line.

I’ll try the print idea, as for the templates, let me send those

{% extends 'base/base.html' %}

{% block content %}

    <h1>Login</h1>
<br/>


        <form action="" method=POST>
                {% csrf_token %}
        <form>
<div class="mb-3">
                          <label for="exampleInputEmail1" class="form-label">Username</label>
                          <input type="text" class="form-control" name="username" aria-describedby="emailHelp">
</div>
<div class="mb-3">
                          <label for="exampleInputPassword1" class="form-label">Password</label>
                          <input type="password" class="form-control" name="password">
        </div>

                        <input type ="submit" value="Submit" class"btn btn-secondary">

                </form>
{% endblock %}

and incase you need it here’s base

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>SITE</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
  </head>
  <body>
    {% include 'base/navbar.html' %}
    <br/>
    <div class="container">
      {% if messages %}
        {% for message in messages %}
          {{ message }}
        {% endfor %}
      {% endif %}
      
      {% block content%}

      {% endblock %}
    </div>  
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
    
  </body>
    
</html>

You have an extra <form> tag. That second tag should be removed.

I did like you asked and edited the form. After which I checked the command prompt and when I submitted the username/password I got the following code (doesn’t matter if password or username is right or wrong)

[02/Sep/2023 20:08:55] "POST /account/login/ HTTP/1.1" 200 3156
Not Found: /favicon.ico
[02/Sep/2023 20:08:55] "GET /favicon.ico HTTP/1.1" 404 2448
Not Found: /favicon.ico
[02/Sep/2023 20:08:56] "GET /favicon.ico HTTP/1.1" 404 2448

Also, I see where you missed having the quotes around POST in the form tag.

Did you add the print statements to your view?

You might also want to add a print(request.method) at that location, too.

Replace above code with this and share the console logs here

def login_account(request):
    print("-------------------------------------")
    print("request ",request)
    print("request.method ",request.method)
    if request.method == "POST":
        print("request.POST ",request.POST)

Not sure if I did it right but this is what I got (Also sorry for the late response I’ve been a bit busy)

[08/Sep/2023 21:53:51] "GET /admin HTTP/1.1" 301 0
[08/Sep/2023 21:53:51] "GET /admin/ HTTP/1.1" 200 8996
[08/Sep/2023 21:53:53] "GET /static/admin/css/base.css HTTP/1.1" 200 21207
[08/Sep/2023 21:53:53] "GET /static/admin/css/dark_mode.css HTTP/1.1" 200 2929
[08/Sep/2023 21:53:53] "GET /static/admin/js/nav_sidebar.js HTTP/1.1" 200 3063
[08/Sep/2023 21:53:53] "GET /static/admin/css/nav_sidebar.css HTTP/1.1" 200 2694
[08/Sep/2023 21:53:53] "GET /static/admin/css/dashboard.css HTTP/1.1" 200 441
[08/Sep/2023 21:53:53] "GET /static/admin/css/responsive.css HTTP/1.1" 200 18533
[08/Sep/2023 21:53:53] "GET /static/admin/img/icon-addlink.svg HTTP/1.1" 200 331
[08/Sep/2023 21:53:53] "GET /static/admin/js/theme.js HTTP/1.1" 200 1943
[08/Sep/2023 21:53:53] "GET /static/admin/img/icon-changelink.svg HTTP/1.1" 200 380
Not Found: /favicon.ico
[08/Sep/2023 21:53:53] "GET /favicon.ico HTTP/1.1" 404 2448
[08/Sep/2023 21:53:58] "GET /account/login/ HTTP/1.1" 200 3158
[08/Sep/2023 21:54:08] "GET /admin/ HTTP/1.1" 200 8996
[08/Sep/2023 21:54:10] "GET /admin/account/user/ HTTP/1.1" 200 9718
[08/Sep/2023 21:54:10] "GET /static/admin/css/changelists.css HTTP/1.1" 200 6566
[08/Sep/2023 21:54:10] "GET /admin/jsi18n/ HTTP/1.1" 200 3343
[08/Sep/2023 21:54:10] "GET /static/admin/js/jquery.init.js HTTP/1.1" 200 347
[08/Sep/2023 21:54:10] "GET /static/admin/js/vendor/jquery/jquery.js HTTP/1.1" 200 292458
[08/Sep/2023 21:54:10] "GET /static/admin/js/core.js HTTP/1.1" 200 5682
[08/Sep/2023 21:54:10] "GET /static/admin/js/admin/RelatedObjectLookups.js HTTP/1.1" 200 8943
[08/Sep/2023 21:54:10] "GET /static/admin/js/actions.js HTTP/1.1" 200 7872
[08/Sep/2023 21:54:10] "GET /static/admin/js/urlify.js HTTP/1.1" 200 7887
[08/Sep/2023 21:54:10] "GET /static/admin/js/prepopulate.js HTTP/1.1" 200 1531
[08/Sep/2023 21:54:10] "GET /static/admin/js/vendor/xregexp/xregexp.js HTTP/1.1" 200 232381
[08/Sep/2023 21:54:10] "GET /static/admin/js/filters.js HTTP/1.1" 200 978
[08/Sep/2023 21:54:10] "GET /static/admin/img/tooltag-add.svg HTTP/1.1" 200 331
[08/Sep/2023 21:54:12] "GET /admin/account/user/1/change/ HTTP/1.1" 200 11750
[08/Sep/2023 21:54:12] "GET /static/admin/css/forms.css HTTP/1.1" 200 9047
[08/Sep/2023 21:54:12] "GET /admin/jsi18n/ HTTP/1.1" 200 3343
[08/Sep/2023 21:54:12] "GET /static/admin/js/calendar.js HTTP/1.1" 200 8466
[08/Sep/2023 21:54:12] "GET /static/admin/js/admin/DateTimeShortcuts.js HTTP/1.1" 200 19319
[08/Sep/2023 21:54:12] "GET /static/admin/js/prepopulate_init.js HTTP/1.1" 200 586
[08/Sep/2023 21:54:12] "GET /static/admin/css/widgets.css HTTP/1.1" 200 11900
[08/Sep/2023 21:54:12] "GET /static/admin/js/change_form.js HTTP/1.1" 200 606
[08/Sep/2023 21:54:12] "GET /static/admin/img/icon-calendar.svg HTTP/1.1" 200 1086
[08/Sep/2023 21:54:22] "POST /account/login/ HTTP/1.1" 200 3158
Not Found: /favicon.ico
[08/Sep/2023 21:54:22] "GET /favicon.ico HTTP/1.1" 404 2448
Not Found: /favicon.ico
[08/Sep/2023 21:54:23] "GET /favicon.ico HTTP/1.1" 404 2448
[08/Sep/2023 21:54:42] "POST /account/login/ HTTP/1.1" 200 3158
Not Found: /favicon.ico
[08/Sep/2023 21:54:42] "GET /favicon.ico HTTP/1.1" 404 2448
Not Found: /favicon.ico
[08/Sep/2023 21:54:43] "GET /favicon.ico HTTP/1.1" 404 2448

Do you mean like this?

<form action="" method="POST">
                {% csrf_token %}

Also I could have sworn I added the print function into the view but maybe I didn’t, either way I tried it again and you can see the results in the reply I left to someone else

Also I’m sorry for the late response I was kinda busy this week

Ok, you’re not calling your view.

Please post your root urls.py file and the urls.py file containing the reference to this view.

Also please repost the current version of the template. (Don’t need the base.)

Don’t worry about delays - we’re here to help.

I’m assuming this is what you mean by root URL file but I’m not sure so correct me if I’m wrong

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("characters/", include("characters.urls")),
    path("account/", include("account.urls")),
    path("account/", include("django.contrib.auth.urls")),
    path('admin/', admin.site.urls),
]

URLs for the account path

from django.urls import path

from . import views

urlpatterns = [
    path('login_account', views.login_account, name="login" ),
]

And current template

{% extends 'base/base.html' %}

{% block content %}

    <h1>Login</h1>
<br/>


        <form action="" method="POST">
                {% csrf_token %}
<div class="mb-3">
                          <label for="exampleInputEmail1" class="form-label">Username</label>
                          <input type="text" class="form-control" name="username" aria-describedby="emailHelp">
</div>
<div class="mb-3">
                          <label for="exampleInputPassword1" class="form-label">Password</label>
                          <input type="password" class="form-control" name="password">
        </div>

                        <input type ="submit" value="Submit" class"btn btn-secondary">

                </form>
{% endblock %}

And again thank you very much it means the most :smiley:

In your log you have (edited):

But, you url is defined as:

which would be accessed by /account/login_account, not /account/login/.

So you’re not getting the page from your view - you’re accessing the default view.

How are you getting to that page?

Ok so,
For starters, I was accessing the page via account/login.
Secondly, I had an idea to try and change everything in the view and the URL to either login_account or log in, it didn’t change anything for me, and it’s giving me the same thing of

[09/Sep/2023 21:27:17] "POST /account/login/ HTTP/1.1" 200 3158
Not Found: /favicon.ico
[09/Sep/2023 21:27:17] "GET /favicon.ico HTTP/1.1" 404 2448
Not Found: /favicon.ico
[09/Sep/2023 21:27:17] "GET /favicon.ico HTTP/1.1" 404 2448

Yes, that’s what’s wrong. You are not accessing your view at that url.

Your view is not defined to respond to /account/login. Your view is defined to respond to /account/login_account as shown by the path defined in your urls.py - as you’ve shown here.

OK SO
Good news is that I was finally able to display an error message on the page itself, so what you brought up actually was the problem, (even though I thought I eliminated that option out of the way but I guess I missed some specific code) so thank you very much!
Bad news is that I’m unable to get a successful login, though this is likely just a problem with the view or model not properly referencing each other.
Either way I got the main problem out of the way so I’m gonna stop asking questions on this thread and either figure out this new problem on my own or just post a separate thread about it, again thank you!