[Python] Django Post request not sent on button click

Discussão em 'Python' iniciado por Stack, Junho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a simple web app in Django and trying to use Ajax so the page will not refresh. The only goal of this app is to have a form that takes some user input and does not refresh when the form is submitted. However, for some reason this does not happen when I click the button. Here is the Index page:

    <!DOCTYPE html>
    <html>
    <body>
    <h2>Create product here</h2>
    <div>
    <form id="new_user_form">
    <div>
    <label for="name" > Name:<br></label>
    <input type="text" id="name"/>
    </div>
    <br/>
    <div>
    <label for="email"> email:<br></label>
    <input type="text" id="email"/>
    </div>
    <div>
    <label for="password" > password:<br></label>
    <input type="text" id="password"/>
    </div>
    <div>
    <input type="submit" value="submitme"/>
    </div>
    </form>
    </body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type = "text/text/javascript">
    $(document).on('submitme', '#new_user_form', function(e)){
    e.preventDefault()
    $.ajax({
    type: 'POST',
    url:'/user/create',
    data:{
    name:$('#name').val(),
    email:$('#email').val(),
    password:$('#password').val(),
    }
    success.function(){
    alert('created')
    }
    })
    }
    </script>
    </html>


    Here is my main urls.py file:

    from django.contrib import admin
    from django.urls import path
    from django.conf.urls import include, url
    from testapp import views
    import testapp
    from django.views.decorators.csrf import csrf_exempt

    urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^$', testapp.views.index),
    url(r'^user/create/$', csrf_exempt(testapp.views.create_user))
    ]


    My views.py file:

    from django.shortcuts import render
    from testapp.models import User
    from django.http import HttpResponse

    # Create your views here.
    def index(request):
    return render(request, 'index.html')

    def create_user(request):
    if request.method == 'POST':
    name = request.POST['name']
    email = request.POST['email']
    password = request.POST['password']

    User.objects.create(
    name = name,
    email = email,
    password = password
    )

    return HttpResponse('')


    And lastly the models.py file:

    from django.db import models

    # Create your models here.
    class User(models.Model):
    name = models.CharField(max_length = 32)
    email = models.EmailField()
    password = models.CharField(max_length = 128)


    The aim of this was when the button is clicked, it should send a POST request to the backend which creates a object of type User and saves it to the database. However, for some reason when I click submit, no POST request is sent according to the Network tool on Chrome. Can someone help me out here?

    Continue reading...

Compartilhe esta Página