![]() |
In Django, a popular Python web framework, URL validation can be easily implemented using built-in tools and libraries. In this article, we will explore how to use the URL validator in Django to ensure that the URLs in your web application are valid and secure. Django’s URL ValidatorDjango provides a URL validator as part of its built-in validation tools. This validator checks whether a given string is valid according to the URL specification. To use this validator, follow these steps: Step 1: Create a project folder, and Navigate to the Project Directorydjango-admin startproject bookstore Activate Virtual Environment (Optional) Step 2: Create a Django AppInside your project, create a Django app named “mini”: python manage.py startapp mini
Step 3: Define the ModelIn this example, we’ll create a simple model to store the validated URLs. Open the models.py file in your url_validator_app folder and define the model as follows: Python3
Step 4: Create form.pyBy defining this form class, we’ve created a structure for capturing and validating URL input from users. It encapsulates the logic needed for URL validation, making it easy to use in our views and templates. Python3
Step 5: Genrate view of the AppThe index view handles URL validation and form submissions, while the success view displays the list of validated URLs. Python3
Step 6: Create the TemplatesCreate two HTML templates: one for the form and another for displaying the validated URLs. Create a templates folder within your app directory and add the following templates: template/index.html: URL Validator HTML
template/index2.html: Validated URLs HTML
Step 7: Configure URLs in the mini/urls.pyConfigure your app’s URLs by creating a urls.py file within the app folder: Python3
Step 8: Incude app URLs in the Project URLsurls.py: In your project’s urls.py, include the URLs from your app: Python3
Output: |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |