This is exactly the purpose of database transactions. All you need to
do is to wrap the block of code in transaction.atomic(), like this::
from django.db import transaction
[...]
if formularioarchivomedios.is_valid():
reader = csv.DictReader(request.FILES['docfile'], delimiter=',')
try:
with transaction.atomic():
for row in reader:
titulo = row['titulo']
url = row['url']
my_file = Medios(titulo=titulo, url=url)
my_file.save()
except WhateverErrorYouWantToHandle as e:
handle_error(e)
Just make sure to handle the error outside of the atomic block; that
way, the transaction will be automatically rolled back if an error
does happen. If you handle the error inside the atomic block, Django
will think there was no error, and it will happily try to commit the
transaction as if everything was all right.
Good luck,
Michal