Django API External API Connect
Django API External API Connect
Django API External API Connect
1. Project Setup:
● Ensure you have a Django project and app set up with the necessary
● Create a form in your app's forms.py file to handle user input. This form will
define the fields (username, password, etc.) the user will enter.
Python
class LoginForm(forms.Form):
password = forms.CharField(widget=forms.PasswordInput,
required=True)
(templates/your_app/). This template will contain the HTML form and logic
HTML
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<h1>Login</h1>
{% csrf_token %} {{ form.as_p }}
<button type="submit">Login</button>
</form>
</body>
</html>
● Explanation:
○ The action attribute of the form element points to the URL pattern
○ The method attribute specifies the HTTP method (POST) to use for
sending data.
LoginForm class.
4. URL Pattern (urls.py):
● In your app's urls.py file, define a URL pattern to map the form submission
Python
urlpatterns = [
● Create a view function in your app's views.py file to handle form submission,
Python
def login_view(request):
if request.method == 'POST':
form = LoginForm(request.POST) # Create form object with
submitted data
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
else:
# Handle login failure (display error message)
else:
● Explanation:
data (request.POST).
○ If the form is valid, it extracts the username and password from the
cleaned data.
There are two main approaches to add a preloader to your Django HTML view for
1. Using JavaScript:
This method involves using JavaScript to display a preloader while the API request is
● Add a container element (e.g., a div) with a hidden class (e.g., hidden) to
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<h1>Login</h1>
<div id="preloader" class="hidden">
<img src="{% static 'images/loading.gif' %}" alt="Loading..."
/>
</div>
<form action="{% url 'your_app:login' %}" method="post">
{% csrf_token %} {{ form.as_p }}
<button type="submit" id="submit-button">Login</button>
</form>
</body>
</html>
clicked.
○ Hide the preloader element once the response from the API is received
JavaScript
// Assuming you have a function to handle the API call in your view
function submitLoginForm() {
// Show preloader
document.getElementById("preloader").classList.remove("hidden");
● No changes are required specifically for the preloader in the view function.
However, the JavaScript code might need to handle the response from the
● This approach utilizes a custom template tag to display the preloader while
the view function processes the form submission. However, it offers less
● Python
register = template.Library()
@register.simple_tag
def show_preloader(is_processing):
"""
Simple template tag to conditionally show/hide preloader based on a
boolean value.
"""
return 'hidden' if not is_processing else ''
●
● b) Modify HTML Template (templates/your_app/login.html):
● HTML
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<h1>Login</h1>
<div id="preloader" class="{{ show_preloader request.processing
}}">
<img src="{% static 'images/loading.gif' %}" alt="Loading..."
/>
</div>
<form action="{% url 'your_app:login' %}" method="post">
{% csrf_token %} {{ form.as_p }}
<button type="submit">Login</button>
</form>
</body>
</html>
●
● c) Django View Function (views.py):
in progress. Set it to True before making the API call and False after
● Python
if request.method == 'POST':
form = LoginForm(request.POST) # ... (existing code)
if form.is_valid():
# Set processing flag to True
request.processing = True
●
●