Skip to content

Commit cc4e6fd

Browse files
Update Lab.md
1 parent 958ea24 commit cc4e6fd

File tree

1 file changed

+0
-147
lines changed

1 file changed

+0
-147
lines changed

Lab.md

Lines changed: 0 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -124,153 +124,6 @@ python manage.py runserver
124124

125125

126126

127-
Django Form Submission Without Page Refresh Using Ajax
128-
Introduction
129-
Web development has evolved significantly, and modern web applications often strive for seamless user experiences. One way to achieve this is by utilizing Ajax (Asynchronous JavaScript and XML) to submit forms without requiring a full page refresh.
130-
131-
In this article, we’ll explore how to implement Django form submission without a page refresh using Ajax.
132-
133-
Step 1: Setting Up the Django Project
134-
Before we begin, ensure you have Django installed. If not, install it using:
135-
136-
sh
137-
Copy code
138-
pip install django
139-
Now, create a new Django project and a Django app:
140-
141-
sh
142-
Copy code
143-
django-admin startproject your_project
144-
cd your_project
145-
python manage.py startapp your_app
146-
Step 2: Creating a Model and Form
147-
Define a Simple Model
148-
In your_app/models.py, create a model to store user data:
149-
150-
python
151-
Copy code
152-
from django.db import models
153-
154-
class YourModel(models.Model):
155-
name = models.CharField(max_length=100)
156-
email = models.EmailField()
157-
# Add other fields as needed
158-
Create a Django Form
159-
In your_app/forms.py, create a form for the model:
160-
161-
python
162-
Copy code
163-
from django import forms
164-
from .models import YourModel
165-
166-
class YourModelForm(forms.ModelForm):
167-
class Meta:
168-
model = YourModel
169-
fields = '__all__'
170-
Step 3: Writing the Views
171-
In your_app/views.py, define a view to handle both form rendering and form submission via Ajax:
172-
173-
python
174-
Copy code
175-
from django.shortcuts import render
176-
from django.http import JsonResponse
177-
from .forms import YourModelForm
178-
179-
def your_view(request):
180-
if request.method == 'POST':
181-
form = YourModelForm(request.POST)
182-
if form.is_valid():
183-
form.save()
184-
return JsonResponse({'message': 'Form submitted successfully!'})
185-
else:
186-
return JsonResponse({'error': 'Form submission failed.'}, status=400)
187-
else:
188-
form = YourModelForm()
189-
return render(request, 'your_template.html', {'form': form})
190-
If it's a POST request, the form is validated and saved, returning a JSON response.
191-
If it's a GET request, the form is rendered normally.
192-
Step 4: Setting Up the Template
193-
Create an HTML template to render the form and handle Ajax requests.
194-
195-
File: your_app/templates/your_template.html
196-
html
197-
Copy code
198-
<!DOCTYPE html>
199-
<html>
200-
<head>
201-
<title>Your Form</title>
202-
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
203-
</head>
204-
<body>
205-
206-
<form id="yourForm" method="post">
207-
{% csrf_token %}
208-
{{ form.as_p }}
209-
<input type="submit" value="Submit">
210-
</form>
211-
212-
<script>
213-
$(document).ready(function() {
214-
$('#yourForm').submit(function(e) {
215-
e.preventDefault();
216-
$.ajax({
217-
type: 'POST',
218-
url: '{% url "your_view_name" %}',
219-
data: $('#yourForm').serialize(),
220-
success: function(response) {
221-
alert(response.message); // Handle success response
222-
},
223-
error: function(response) {
224-
alert(response.error); // Handle error response
225-
}
226-
});
227-
});
228-
});
229-
</script>
230-
231-
</body>
232-
</html>
233-
This loads jQuery for handling Ajax.
234-
When the form is submitted, it sends data asynchronously to the server without refreshing the page.
235-
Step 5: Configuring URLs
236-
Modify your_project/urls.py
237-
python
238-
Copy code
239-
from django.contrib import admin
240-
from django.urls import path, include
241-
242-
urlpatterns = [
243-
path('admin/', admin.site.urls),
244-
path('your_app/', include('your_app.urls')),
245-
]
246-
Add a URL Pattern in your_app/urls.py
247-
python
248-
Copy code
249-
from django.urls import path
250-
from .views import your_view
251-
252-
urlpatterns = [
253-
path('your-view/', your_view, name='your_view_name'),
254-
]
255-
Step 6: Running the Django Server
256-
Now, apply migrations and start the Django development server:
257-
258-
sh
259-
Copy code
260-
python manage.py migrate
261-
python manage.py runserver
262-
Conclusion
263-
When you navigate to:
264-
265-
arduino
266-
Cop
267-
http://127.0.0.1:8000/your_app/your-view/
268-
you should see the form. Upon submission, the data will be sent to the server using Ajax, and the page will not refresh.
269-
270-
This approach enhances user experience by providing instant feedback without reloading the entire page.
271-
272-
🚀 Congratulations! You've successfully implemented Django form submission without page refresh using Ajax! Let me know if you have any questions.
273-
274127

275128
### lab 2
276129
- Steps to Build a Debugging Web Application in Django

0 commit comments

Comments
 (0)