Fadi
Fadi

Reputation: 69

Django error. module 'django.http.request' has no attribute 'POST'

I am new to Django and I am trying to create a simple web application. I've created a login form and added a hyperlink for signing up for new users. Unfortunately, I got the following error. module 'django.http.request' has no attribute 'POST'

In the bellow you will find view.py code:

from django.shortcuts import render
from django.http import HttpResponse, request
from django.db import connection
from django.contrib.auth.decorators import login_required
import pyodbc

def index(request):
     if 'Login' in request.POST:
          rows = []
          username = request.POST.get('username')
          if username.strip() != '':
               rows = getLogin(username)
               if len(rows) > 0:
                    return render(request, 'login/welcome.html')
               else:
                    return render (request, 'login/index.html')
          else:
              return render (request, 'login/index.html') 
     else:
          return render (request, 'login/index.html')

def newUser(requset):
     if 'NewUser' in request.POST:
          return render(request,'login/newuser.html')

def getLogin(UserName=''):
     command = 'EXEC GetLogin\'' + UserName + '\''
     cursor = connection.cursor()
     cursor.execute(command)

     rows = []
     while True:
          row = cursor.fetchone()
          if not row:
               break
          
          userName = row[0]
          password = row[1]
          name = row[2]
          email = row[3]
                   
          rows.append({'userName': userName, 'password': password, 'name': name, 'email': email})
     
     cursor.close()
     return rows

@login_required(login_url='/')
def readLogin(request):
     if request.user.is_authenticated:
          rows = getLogin()
          return render(request, 'login/loginsdata.html', {'rows': rows})
     else:
          return HttpResponse('You are not authenticated</br>')

Here urls.py code of the login app:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('logindata/', views.readLogin, name='logindata'),
    path('newuser/', views.newUser, name='newuser'),

]

Here newuser.html code:

{% block content %}
<form method="POST">
{% csrf_token %}
    <table>
        <tr>
            <td>
                <lable>Name </lable>
            </td>
            <td>
                <input type="text" name="name">
            </td>
        </tr>
        <tr>
            <td>
                <lable>Username </lable>
            </td>
            <td>
                <input type="text" name="username">
            </td>
        </tr>
        <tr>
            <td>
                <lable>Password </lable>
            </td>
            <td>
                <input type="password" name="password">
            </td>
        </tr>  
        <tr>
            <td>
                <lable>Confirm password </lable>
            </td>
            <td>
                <input type="password" name="confirmPassword">
            </td>
        </tr>  
        <tr>
            <td>
                <lable>email </lable>
            </td>
            <td>
                <input type="email" name="email">
            </td>
        </tr>       
        <tr>
            <td>
                <input type="submit" name="save" value="Save" colspan=2>
            </td>
        </tr>    
</form>
{% endblock content %}

And here you will find the index.html page:

{% block content %}    
    <table>
        <tr>
            <td/>
            <td/>
            <td/>
        </tr>
        <tr>
            </td>
            <td>
                <form method="POST">
                    {% csrf_token %}
                    <table>
                        <tr>
                            <td>
                                <lable>Username </lable>
                            </td>
                            <td>
                                <input type="text" name="username" placeholder="Username">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <lable>Password </lable>
                            </td>
                            <td>
                                <input type="password" name="password" placeholder="Password">
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <input type="submit" value="Login" name="Login">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <a href="{% url 'newuser' %}" name="NewUser">Sign Up</a>
                            </td>
                            <td>
                                <a href="">Forget my password</a>
                            </td>
                        </tr>
                    </table>
                </form>
            </td>
            </td>
        </tr>
        <tr>
            <td/>
            <td/>
            <td/>
        </tr>    
    </table>
{% endblock content %}

Upvotes: 0

Views: 757

Answers (2)

user8193706
user8193706

Reputation: 2425

After corrected from requset to request as @gasman pointed. Your views.py for newUser will be

def newUser(request):
    if 'NewUser' in request.POST:
        return render(request,'login/newuser.html')

After this you pointed out that you got

The view login.views.newUser didn't return an HttpResponse object

It is because there is no else part in your code. So if your if condition fails then your view function did not return any response that is why it says didn't return an HttpResponse object. So return HttpResponse if your if condition fails and it will be fine.

def newUser(request):
    if 'NewUser' in request.POST:
        return render(request,'login/newuser.html')
    else:
        return HttpResponse() #<-------- if else condition fails.

In your case why it fail's

Because in your newuser.html your submit button is as follows.

<input type="submit" name="save" value="Save" colspan=2>

And in your views you are comparing the value NewUser with request.POST. So your if condition fails and you are getting this error.

So either change your name as if 'save' in request.POST in your views.py. Or change name="save" with name="NewUser" in your newuser.html your problem will be solved.

Upvotes: 1

Marco
Marco

Reputation: 2834

You should check if the request is a post. So instead of

if 'Login' in request.POST:

you should check it the following way:

if request.method == "POST":

Upvotes: 0

Related Questions