
PYTHON — -Removing URL in Python-
Programs must be written for people to read, and only incidentally for machines to execute. — Harold Abelson

PYTHON — String Slicing Exercise in Python
Deleting a URL in Python
In this lesson, you’ll learn how to delete a URL in Python using FastAPI. When building a URL shortener app, it’s essential to provide functionality to delete shortened URLs. This ensures that unnecessary URLs can be removed from the system. Here’s a step-by-step guide on how to implement URL deletion functionality in Python using FastAPI.
Step 1: Create a Function to Deactivate the URL
To start, create a function in crud.py to deactivate the URL by its secret key. The purpose of deactivating instead of deleting the URL is to maintain a level of control and allow for potential reactivation if needed. The function deactivate_db_url_by_secret_key() will set the .is_active attribute of the URL to False.
# crud.py
def deactivate_db_url_by_secret_key(secret_key: str):
# Deactivate the URL by setting .is_active attribute to False
# Add code here to update the databaseStep 2: Create a Function to Delete the URL
Additionally, create a function in crud.py to delete the URL by its secret key. This function will allow for the complete deletion of the URL from the database.
# crud.py
def delete_db_url_by_secret_key(secret_key: str):
# Delete the URL from the database
# Add code here to delete the URLStep 3: Implement the Endpoint to Delete the URL
Next, add an endpoint in main.py to handle the deletion of the URL. Use the @app.delete() decorator to indicate that the delete_url() function accepts DELETE requests. The delete_url() function should require the appropriate secret_key in the request body.
# main.py
from fastapi import FastAPI
app = FastAPI()
@app.delete("/delete_url/{secret_key}")
async def delete_url(secret_key: str):
# Check if the request contains the correct secret_key
# If valid, call the function to delete the URL
# If not valid, return an error messageStep 4: Complete the Deletion Process
Inside the delete_url() function, call the deactivate_db_url_by_secret_key() function from crud.py to deactivate the URL. If the deactivation is successful, return a success message. Otherwise, handle the error appropriately, indicating that the URL was not found.
# main.py
@app.delete("/delete_url/{secret_key}")
async def delete_url(secret_key: str):
db_url = crud.deactivate_db_url_by_secret_key(secret_key)
if db_url:
return {"message": "URL successfully deleted"}
else:
return {"error": "URL not found"}By following these steps, you can implement URL deletion functionality in a Python FastAPI application. This functionality allows the control and management of shortened URLs, giving users the ability to deactivate and potentially reactivate URLs as needed.
In conclusion, by implementing these functions and endpoints, you can create a fully functional URL shortener app with the ability to delete and manage URLs effectively.
Now that you’ve learned how to delete a URL in Python using FastAPI, you can further explore the other aspects of building a URL shortener application.







