<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ajax with Django</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
/* General body styling */
body {
font-family: 'Arial', sans-serif;
background-color: #f0f4f8;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
/* Center the main container and set its width */
.container {
background-color: #ffffff;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
width: 350px;
text-align: center;
}
h1 {
color: #333;
margin-bottom: 1.5rem;
font-size: 1.8rem;
font-family: Verdana, Geneva, Tahoma, sans-serif
}
/* Style for form input */
input[type="text"] {
width: 100%;
padding: 0.75rem;
font-size: 1rem;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 1rem;
font-family: Verdana, Geneva, Tahoma, sans-serif;
outline: none;
transition: border-color 0.3s ease;
}
input[type="text"]:focus {
border-color: #007bff;
}
/* Styling for submit button */
button[type="submit"] {
width: 100%;
background-color: rgb(10, 130, 10);
color: #fff;
padding: 0.75rem;
font-size: 1rem;
font-weight: bold;
font-family: Verdana, Geneva, Tahoma, sans-serif;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
/* Style for displaying the response message */
#responseMessage {
margin-top: 1.5rem;
font-size: 1.2rem;
color: black;
font-weight: bold;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
/* Subtle animations */
input[type="text"], button[type="submit"] {
transition: all 0.3s ease;
}
/* Responsiveness */
@media (max-width: 768px) {
.container {
width: 90%;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Ajax with Django</h1>
<form id="myForm">
<input type="text" id="nameInput" name="name" placeholder="Enter your name">
<button type="submit">Submit</button>
</form>
<div id="responseMessage"></div>
</div>
<script>
// Ajax setup for form submission
$(document).ready(function() {
$('#myForm').on('submit', function(event) {
event.preventDefault();
$.ajax({
url: '/ajax/', // The URL of the Django view that handles the request
type: 'POST',
data: {
'name': $('#nameInput').val(),
'csrfmiddlewaretoken': '{{ csrf_token }}' // CSRF token for security
},
success: function(response) {
$('#responseMessage').text(response.message); // Update the page dynamically
}
});
});
});
</script>
</body>
</html>